Skip to content

Commit 674dad1

Browse files
authored
Add files via upload
1 parent 5ff05c5 commit 674dad1

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

pwmGenerator.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <p18f452.h>
2+
#pragma config WDT = OFF
3+
#define MAX_HIGH_LOW 0xB1E0 // GENERATING 20 MS PERIOD
4+
5+
unsigned char timer0 = 0x00, button = 0x00, change = 0x00;
6+
unsigned int multiByteOld = 0x00, multiByteNew = 0x00, interm = 0x00;
7+
8+
#pragma interrupt ISR
9+
void ISR(void)
10+
{
11+
if(INTCONbits.INT0IF)
12+
{
13+
change = 0x01;
14+
INTCONbits.INT0IF = 0;
15+
if(!button)
16+
{
17+
button = 0x01;
18+
multiByteNew = 0xB9B0; // 90 DUTY CYCLE
19+
multiByteOld = 0xC568;
20+
}
21+
else if(button == 0x01)
22+
{
23+
button = 0x02;
24+
multiByteNew = 0xC568; // 75 DUTY CYCLE
25+
multiByteOld = 0xB9B0;
26+
}
27+
else if(button == 0x02)
28+
{
29+
button = 0x03;
30+
multiByteNew = 0xD8F0; // 50 DUTY CYCLE
31+
multiByteOld = 0xC568;
32+
}
33+
else if(button == 0x03)
34+
{
35+
button = 0x04;
36+
multiByteNew = 0xEC78; // 25 DUTY CYCLE
37+
multiByteOld = 0xD8F0;
38+
}
39+
else if(button == 0x04)
40+
{
41+
button = 0x00;
42+
multiByteNew = 0xF830; // 5% DUTY CYCLE
43+
multiByteOld = 0xEC78;
44+
}
45+
}
46+
else if(INTCONbits.TMR0IF)
47+
{
48+
INTCONbits.TMR0IF = 0;
49+
if(!timer0)
50+
{
51+
timer0 = 0x01;
52+
TMR0H = (multiByteNew & 0xFF00) >> 8;
53+
TMR0L = (multiByteNew & 0x00FF);
54+
LATDbits.LATD0 = 1;
55+
T0CONbits.TMR0ON = 1;
56+
}
57+
else
58+
{
59+
if(change) // IF INT0 IS PRESSED THEN THE NEW DUTY CYCLE WILL BE UPDATED AFTER THE END OF THE CURRENT PERIOD
60+
{
61+
interm = multiByteNew;
62+
multiByteNew = multiByteOld;
63+
}
64+
timer0 = 0x00;
65+
TMR0H = ((MAX_HIGH_LOW - multiByteNew) & 0xFF00) >> 8; // SUBTRUCT THE HIGH PERIOD FROM THE WHOLE PERIOD FOR LOW PORTION
66+
TMR0L = ((MAX_HIGH_LOW - multiByteNew) & 0x00FF);
67+
LATDbits.LATD0 = 0;
68+
if(change)
69+
{
70+
multiByteNew = interm;
71+
change = 0x00;
72+
}
73+
}
74+
}
75+
}
76+
77+
#pragma code VECTOR = 0x00008 // ISR WITH AT ADDRESS 0x08 (HIGH PRIORITY)
78+
void VECTOR(void)
79+
{
80+
_asm
81+
GOTO ISR
82+
_endasm
83+
}
84+
#pragma code
85+
86+
void main(void)
87+
{
88+
TRISDbits.TRISD0 = 0; // OUTPUT RD0
89+
INTCONbits.GIE = 1; // GLOBAL INTERRUPT ENABLE BIT
90+
INTCONbits.INT0IE = 1;
91+
INTCONbits.INT0IF = 0;
92+
INTCONbits.TMR0IE = 1;
93+
T0CON = 0x08;
94+
INTCONbits.INT0IF = 1; // TRIGGER AN EXTERNAL HARDWARE INTERRUPT BY A SOFTWARE
95+
INTCONbits.TMR0IF = 1; // TRIGGER TIMER INTERRUPT BY A SOFTWARE
96+
while(1);
97+
}

0 commit comments

Comments
 (0)