void main()
{
while(1)
{
status_flag=1;
PWM=0;
msdelay(5000);
status_flag=0;
motor=0;
msdelay(5000);
}
void pwm_setup()
{
TMOD &= 0xF0; // Clear 4bit field for timer0
TMOD |= 0x01; // Set timer0 in mode 1 = 16bit mode
TH0 = 0x00; // First time value
TL0 = 0x00; // Set arbitrarily zero
ET0 = 1; // Enable Timer0 interrupts
EA = 1; // Global interrupt enable
TR0 = 1; // Start Timer 0
}
void timer0_ISR(void) interrupt 1
{
TR0 = 0; // Stop Timer 0
if((PWM_flag==1) && (status_flag==1))
{
PWM_flag=0;
motor = 0;
temp = (255-PWM) * PWM_Freq_Num;
TH0 = 0xFF - (temp>>8)&0xFF;
TL0 = 0xFF - temp&0xFF;
}
else if((PWM_flag==0) && (status_flag==1))
{
PWM_flag=1;
motor = 1;
temp = PWM * PWM_Freq_Num;
TH0 = 0xFF - (temp>>8)&0xFF;
TL0 = 0xFF - temp&0xFF;
}
TF0 = 0; // Clear the interrupt flag
TR0 = 1; // Start Timer 0
}
#include "REG51.H"
#include "STDIO.h"
sbit motor=P0^7; // MOTOR
bit PWM_flag = 0;
void msdelay(unsigned int value);
unsigned char PWM = 0;
unsigned int temp = 0;
bit status_flag;
#define PWM_Freq_Num 1
void main()
{
while(1)
{
status_flag=1;
PWM=0;
msdelay(5000);
status_flag=0;
motor=0;
msdelay(5000);
}
} //Main Ends Here
void msdelay(unsigned int value)
{
unsigned int i,j;
for(i=0;i<value;i++)
for(j=0;j<100;j++);
}
void pwm_setup()
{
TMOD &= 0xF0; // Clear 4bit field for timer0
TMOD |= 0x01; // Set timer0 in mode 1 = 16bit mode
TH0 = 0x00; // First time value
TL0 = 0x00; // Set arbitrarily zero
ET0 = 1; // Enable Timer0 interrupts
EA = 1; // Global interrupt enable
TR0 = 1; // Start Timer 0
}
void timer0_ISR(void) interrupt 1
{
TR0 = 0; // Stop Timer 0
if((PWM_flag==1) && (status_flag==1))
{
PWM_flag=0;
motor = 0;
temp = (255-PWM) * PWM_Freq_Num;
TH0 = 0xFF - (temp>>8)&0xFF;
TL0 = 0xFF - temp&0xFF;
}
else if((PWM_flag==0) && (status_flag==1))
{
PWM_flag=1;
motor = 1;
temp = PWM * PWM_Freq_Num;
TH0 = 0xFF - (temp>>8)&0xFF;
TL0 = 0xFF - temp&0xFF;
}
TF0 = 0; // Clear the interrupt flag
TR0 = 1; // Start Timer 0
}
Code C - [expand] 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 #include "REG51.H" #include "STDIO.h" sbit motor=P0^7; // MOTOR bit PWM_flag = 0; void msdelay(unsigned int value); unsigned char PWM = 0; unsigned int temp = 0; bit status_flag; #define PWM_Freq_Num 1 void main() { while(1) { status_flag=1; PWM=0; msdelay(5000); status_flag=0; motor=0; msdelay(5000); } } //Main Ends Here void msdelay(unsigned int value) { unsigned int i,j; for(i=0;i<value;i++) for(j=0;j<100;j++); } void pwm_setup() { TMOD &= 0xF0; // Clear 4bit field for timer0 TMOD |= 0x01; // Set timer0 in mode 1 = 16bit mode TH0 = 0x00; // First time value TL0 = 0x00; // Set arbitrarily zero ET0 = 1; // Enable Timer0 interrupts EA = 1; // Global interrupt enable TR0 = 1; // Start Timer 0 } void timer0_ISR(void) interrupt 1 { TR0 = 0; // Stop Timer 0 if((PWM_flag==1) && (status_flag==1)) { PWM_flag=0; motor = 0; temp = (255-PWM) * PWM_Freq_Num; TH0 = 0xFF - (temp>>8)&0xFF; TL0 = 0xFF - temp&0xFF; } else if((PWM_flag==0) && (status_flag==1)) { PWM_flag=1; motor = 1; temp = PWM * PWM_Freq_Num; TH0 = 0xFF - (temp>>8)&0xFF; TL0 = 0xFF - temp&0xFF; } TF0 = 0; // Clear the interrupt flag TR0 = 1; // Start Timer 0 }
Why are you too lazy to do simple calculations on your own?temp = PWM * PWM_Freq_Num;
TH0 = 0xFF - (temp>>8)&0xFF;
TL0 = 0xFF - temp&0xFF;
What duties PWM do you want? Are they are some fixed duties or do you want 0% to 100% variable duties?
I need to reach 100% from 50 in 18 steps.How are you going to vary the PWM duties in your final device? Using a button to increment and another one to decrement?
And between 50 to 100% duties do you want 1% duty steps?
How are you going to vary the PWM duties in your final device? Using a button to increment and another one to decrement?
And between 50 to 100% duties do you want 1% duty steps?
Okay? And what should be the PWM frequency? Can I choose any PWM frequency between 500 Hz and 2kHZ?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?