summaryrefslogtreecommitdiff
path: root/2004/n/fpga/src/pwm/comptvalue3.vhd
blob: 0e32003d5a031b53cd7f38cfc453516cc804f6a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
-------------------------------------------------------------------------------
--comptvalue3.vhd 
--Eurobot 2004 : APB Team
--Auteur : Fid�le GAFAN
--Module g�n�rateur des PWM
--
--REMARQUE(S):changer tccompt,q et valuecompt
--            si CLK#32MHz et/ou qu'on modifie les valeurs de r�f�rence de T1
--            et T2 
-- Tcmax=20ms/1us=20161cycles.
--*Si DATACOMPT=0,on veut que T2 vale 0,5ms donc on initialise Q � la valeur
--Q=0,5ms/1us=505.
--*Si DATACOMPT=255,on veut que T2 vale 1,5ms donc on initialise Q � la valeur
--Q=1,5ms/1us=1515.
--*Pour toute autre valeur de DATACOMPT comprise entre les deux pr�c�dentes et 
--diff�rentes de ces derni�res,on initialise Q avec
--Q=(0,5ms/1us)+(DATACOMPT*min[((1,5ms-0,5ms)/1us)/(255-1)]
-------------------------------------------------------------------------------
--LIBRARY
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
--ENTITY
entity comptvalue3 is
	port (
                RST		: in std_logic;
                CLK		: in std_logic;
                CLK1USCOMPT	: in std_logic;
		loadcompt	: in std_logic;
		datacompt	: in integer range 0 to 255;
		tccompt		: buffer integer range 0 to 20161;  --nb de cycles de clk1us
                                                                    --pour avoir 19,999712ms
		valuecompt	: out integer range 0 to 1000000 --duree pdt laql la sortie
                                                                 --reste haute. Decrementee
	);
end entity;

architecture comptvalue3behav of comptvalue3 is 

	signal q		: integer range 0 to 1000000; --recevra duree courante de valuecompt pdt laql la sortie
                                           --reste haute. Decrementee
		
begin
	process(RST,CLK)
constant tcmax		:integer range 0 to 20161:=20161;
	begin
	if (RST = '1') then             --initialisation horloge 19,999712ms et
                                        --premier chargement sans load;role du
                                        --state1.Prochain etat : calcul ie state3
          tccompt<=tcmax;
          if (datacompt=0) then
            q<=505; --pour data=0 la periode doit durer 0,5ms
	  elsif (datacompt=255) then 
            q<=1515;--pour data=255 la periode doit durer 1,5ms
	  elsif((datacompt/=0)and (datacompt/=255)) then
            q<=(505+(datacompt*3));
          end if;
        elsif (CLK'event and CLK = '1') then
          if (CLK1USCOMPT='1') then
            if (loadcompt = '1') then   --state2 chargement pendant 1 clk1us
                tccompt<=tcmax;         --pdt 1 clk1us, tccompt reste max
                if 	(datacompt=0) then 
			q<=505;--pour data=0 la periode doit durer 0,5ms
		elsif 	(datacompt=255) then 
			q<=1515;--pour data=255 la periode doit durer 1,5ms
		elsif 	((datacompt/=0)and (datacompt/=255)) then
                  q<=(505+(datacompt*3));
		end if;
             elsif (loadcompt='0') then --state3 calculs a chaque front de clk1us
               if (q/=0) then
                 q<=(q-1);
                 tccompt<=((tccompt)-1);
               elsif (q=0) then
                 q<=0;
                 tccompt<=((tccompt)-1);
	       end if;
             end if;--if du loadcompt
           end if;--if  du clk1us
	end if;--elsif du clk et rst
       	end process;
	valuecompt <= q;
end comptvalue3behav;