diff options
author | saurabhb17 | 2019-11-28 12:26:12 +0530 |
---|---|---|
committer | GitHub | 2019-11-28 12:26:12 +0530 |
commit | 96c30a142de5fe48e9765934eb5a073ea9318cf4 (patch) | |
tree | fcac92561a3faf38ad74b9b68ebfb0b81000ee60 /Example | |
parent | f9c1cefc99ecd38583e0fb32b5613632bc7ebf2d (diff) | |
parent | d2411e6515fbd81c325fcbf34113e04a121ec694 (diff) | |
download | nghdl-96c30a142de5fe48e9765934eb5a073ea9318cf4.tar.gz nghdl-96c30a142de5fe48e9765934eb5a073ea9318cf4.tar.bz2 nghdl-96c30a142de5fe48e9765934eb5a073ea9318cf4.zip |
Merge pull request #33 from saurabhb17/master
VHDL codes for PWM
Diffstat (limited to 'Example')
-rw-r--r-- | Example/PWM/pwmdecrement.vhdl | 41 | ||||
-rw-r--r-- | Example/PWM/pwmincrement.vhdl | 41 | ||||
-rw-r--r-- | Example/README.md | 9 |
3 files changed, 91 insertions, 0 deletions
diff --git a/Example/PWM/pwmdecrement.vhdl b/Example/PWM/pwmdecrement.vhdl new file mode 100644 index 0000000..b58ab86 --- /dev/null +++ b/Example/PWM/pwmdecrement.vhdl @@ -0,0 +1,41 @@ +library ieee; +use ieee.std_logic_1164.all; +use ieee.numeric_std.all; + +entity pwmdecrement is +port(C : in std_logic; + D : in std_logic; + Q : out std_logic); +end pwmdecrement; + + +architecture bhv of pwmdecrement is + signal count: integer:=0; + signal alpha: integer:=0; --counts number of clocks + signal beta : integer:=1; --counts number of times 'D' is low w.r.t Clock signal(C) + signal tmp : integer:=9; --stores the value of beta for which Q will be set HIGH + begin + process (C,D) + + begin + if(C='1' and C'event) then + alpha<=alpha+1; --counts number of rising edges + if(count=0) then --initial pulse + Q <= '1'; + count<=1; + end if; + if(D='0') then --if D is low, increase beta by 1 + beta<=beta+1; + end if; + if(alpha=9) then --when aplha is 9, decrease beta by 1 and store in tmp //--set to 9 on purpose(so that first pwm signal has 90% duty cycle) + tmp<=beta-1; --decrease beta and store in tmp, so that we get 10% less duty cycle than previous + alpha<=0; + beta<=0; + count<=0; + end if; + if(tmp=alpha) then --the moment when number of clocks(alpha) equals previous number of times Q was high(tmp), we turn Q off. + Q<='0'; + end if; + end if; + end process; +end bhv; diff --git a/Example/PWM/pwmincrement.vhdl b/Example/PWM/pwmincrement.vhdl new file mode 100644 index 0000000..fd84d37 --- /dev/null +++ b/Example/PWM/pwmincrement.vhdl @@ -0,0 +1,41 @@ +library ieee; +use ieee.std_logic_1164.all; +use ieee.numeric_std.all; + +entity pwmincrement is +port(C : in std_logic; + D : in std_logic; + Q : out std_logic); +end pwmincrement; +--working code for incrementing duty cycle \ Output of oscillator is fed back using a LPF and Comparator in F/B loop +architecture bhv of pwmincrement is + signal count: integer:=0; + signal alpha: integer:=0; --counts number of clocks + signal beta : integer:=0; --counts number of times 'D' is low w.r.t Clock signal(C) + signal tmp : integer:=0; --stores the value of beta for which Q will be set HIGH + begin + process (C,D) + + begin + if(C='1' and C'event) then + alpha<=alpha+1; --counts number of rising edges + if(count=0) then --initial pulse + Q <= '1'; + count<=1; + end if; + if(D='0') then --if D is low, increase beta by 1 + beta<=beta+1; + end if; + if(alpha=8) then --when aplha is 8, incremement beta by 1 and store in tmp //--set to 8 on purpose(so that first pwm signal has 10% duty cycle) + tmp<=beta+1; --increase beta and store in tmp, so that we get 10% more duty cycle than previous + alpha<=0; --reset alpha + beta<=0; --reset beta + count<=0; --reset count + end if; + if(tmp=alpha) then --the moment when number of clocks(alpha) equals previous number of times Q was high(tmp), we turn Q off. Effectively incrementing duty cycle by 10% + Q<='0'; + tmp<=0; + end if; + end if; + end process; +end bhv; diff --git a/Example/README.md b/Example/README.md new file mode 100644 index 0000000..e3eb0cf --- /dev/null +++ b/Example/README.md @@ -0,0 +1,9 @@ +Instructions on how to use the examples provided here: +1. Go to eSim main window -> Click on NGHDL icon from the left toolbar, click on the 'browse' button, go to ../nghdl/Example/ and locate which example you wish to simulate. +2. After opening the directory of desired example, locate the vhdl file , click on the "Open" button at the bottom of "Open File" window. +3. Click on 'upload' button in the NGHDL pop-up window. File will be processed in the backend for few seconds. Now exit the NGHDL window. +4. Open the desired example in eSim using the Open Project button, double click on it when the project is loaded in the "Projects" window. +5. Click on the "Simulation" button on eSim Main window. + +NGHDL feature is still under development. More examples will be added by eSim team along the way. +If you have a good command on VHDL and electronics, please feel free to contribute. |