-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwmAndSerial.c
75 lines (66 loc) · 1.69 KB
/
pwmAndSerial.c
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
#include <stdio.h>
#include <stdlib.h>
#include <p89lpc9351.h>
#define XTAL 7373000L
#define BAUD 115600L
//We want timer 0 to interrupt every 100 microseconds ((1/10000Hz)=100 us)
#define FREQ 10000L
//The reload value formula comes from the datasheet...
#define TIMER0_RELOAD_VALUE (65536L-((XTAL)/(2*FREQ)))
//These variables are used in the ISR
volatile unsigned char pwmcount;
volatile unsigned char pwm1;
//Procedures
void putchar (char c)
{
while (!TI);
TI=0;
SBUF=c;
}
char getchar (void)
{
while (!RI);
RI=0;
return SBUF;
}
void InitSerialPort(void)
{
BRGCON=0x00; //Make sure the baud rate generator is off
BRGR1=((XTAL/BAUD)-16)/0x100;
BRGR0=((XTAL/BAUD)-16)%0x100;
BRGCON=0x03; //Turn-on the baud rate generator
SCON=0x52; //Serial port in mode 1, ren, txrdy, rxempty
P1M1=0x00; //Enable pins RxD and Txd
P1M2=0x00; //Enable pins RxD and Txd
}
void InitTimer0 (void)
{
// Initialize timer 0 for ISR 'pwmcounter' below
TR0=0; // Stop timer 0
TF0=0; // Clear the overflow flag
TMOD=(TMOD&0xf0)|0x01; // 16-bit timer
TH0=TIMER0_RELOAD_VALUE/0x100;
TL0=TIMER0_RELOAD_VALUE%0x100;
TR0=1; // Start timer 0
ET0=1; // Enable timer 0 interrupt
EA=1; // Enable global interrupts
}
//Interrupt 1 is for timer 0. This function is executed every 100 us.
void pwmcounter (void) interrupt 1 using 1
{
TF0=0; // Clear the overflow flag
//Reload the timer
TR0=0; // Stop timer 0
TH0=TIMER0_RELOAD_VALUE/0x100;
TL0=TIMER0_RELOAD_VALUE%0x100;
TR0=1; // Start timer 0
if(++pwmcount>99) pwmcount=0;
P1_6=(pwm1>pwmcount)?1:0;
}
void main (void)
{
InitSerialPort();
InitTimer0();
pwm1=20; //50% duty cycle wave at 100Hz
printf("Hello!\n\rPlease check pin P1.6 with the oscilloscope!\n\r");
}