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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
| #include <lpc177x_8x.h>
#define RES 150000
#define a 14
#define b 60
#define ONE_SEC_SUITCH 20000
//***********************************************************************
_Bool green_flag=1;
int complate_green_flag=0;
_Bool red_flag=0;
int complate_red_flag=0;
//***********************************************************************
uint8_t counter_color=0;
uint8_t red[25] ={18,18,18,18,18,18,18,18,18,54,54,54,54,54,54,54,54,18,18,18,18,18,18,18,18};
uint8_t green[25] ={54,54,54,54,54,54,54,54,54,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18};
uint8_t yellow[25] ={54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,18,18,18,18,18,18,18,18};
uint8_t blue[25] ={18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,54,54,54,54,54,54,54,54};
uint8_t blue1[25] ={a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,b,b,b,b,b,b,b,b};
uint8_t double_green[49] ={54,54,54,54,54,54,54,54,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,54,54,54,54,54,54,54,54,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18};
uint8_t double_red[49] ={18,18,18,18,18,18,18,18,54,54,54,54,54,54,54,54,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,54,54,54,54,54,54,54,54,18,18,18,18,18,18,18,18};
uint8_t double_blue[49] ={18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,54,54,54,54,54,54,54,54,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,54,54,54,54,54,54,54,54};
uint8_t green_red[49] ={54,54,54,54,54,54,54,54,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,54,54,54,54,54,54,54,54,18,18,18,18,18,18,18,18};
//***********************************************************************
void initTimer0(void)
{
LPC_TIM0->CTCR = 0x0;
LPC_TIM0->PR = 0;
LPC_TIM0->TCR = 0x02;
}
//***********************************************************************
void delay_us(unsigned int microseconds)
{
LPC_TIM0->TCR = 0x02;
LPC_TIM0->TCR = 0x01;
while(LPC_TIM0->TC < microseconds);
LPC_TIM0->TCR = 0x00;
}
//***********************************************************************
void initPWM(void)
{
LPC_SC->PCONP |= 1<<5;
LPC_IOCON->P1_2 = 3; //Select PWM0.1 output for Pin1.18
LPC_PWM0->PCR = 0x0; //Select Single Edge PWM - by default its single Edged so this line can be removed
LPC_PWM0->PR = 0; //1 micro-second resolution
LPC_PWM0->MR0 = 75; //20000us = 20ms period duration
LPC_PWM0->MR1 = 54; //1ms - default pulse duration i.e. width
LPC_PWM0->MCR |= (1<<1) | (1<<0) ; //Reset PWM TC on PWM0MR0 match
LPC_PWM0->LER = (1<<1) | (1<<0); //update values in MR0 and MR1
LPC_PWM0->PCR = (1<<9); //enable PWM output
LPC_PWM0->TCR = (1<<1); //Reset PWM TC & PR
LPC_PWM0->TCR = (1<<0) | (1<<3); //enable counters and PWM Mode
}
//***********************************************************************
void updatePulseWidth(unsigned int pulseWidth)
{
LPC_PWM0->MR1 = pulseWidth; //Update MR1 with new value
LPC_PWM0->LER = (1<<1); //Load the MR1 new value at start of next cycle
}
//***********************************************************************
// void switch_red_green()
// {
// if(green_flag)
// {
// green();
// }
// if(complate_green_flag>ONE_SEC_SUITCH)
// {
// green_flag=0;
// red_flag=1;
// complate_green_flag=0;
// }
// if(red_flag)
// {
// red();
// }
// if(complate_red_flag>ONE_SEC_SUITCH)
// {
// red_flag=0;
// green_flag=1;
// complate_red_flag=0;
// }
// }
//***********************************************************************
void send_color (uint8_t color[25])
{
updatePulseWidth(color[counter_color]);
if(counter_color >=25 )
{
updatePulseWidth(0);
delay_us(RES);
counter_color=0;
}
}
//***********************************************************************
void send_double_color (uint8_t color[49])
{
updatePulseWidth(color[counter_color]);
if(counter_color >=48 )
{
updatePulseWidth(0);
delay_us(RES);
counter_color=0;
}
}
//***********************************************************************
void PWM0_IRQHandler(void)
{
LPC_PWM0->IR |= (1<<0);
counter_color++;
}
//***********************************************************************
int main()
{
initTimer0();
initPWM();
NVIC_EnableIRQ(PWM0_IRQn);
while(1)
{
//for(int t=0;t<100000;t++)
send_color( blue );
//send_double_color(green_red);
//send_double_color(double_blue);
//send_double_color(double_green);
//send_double_color(double_red);
}
} |