|
【瑞萨电子MCU套件免费试用】自行车炫彩风火轮开发应用笔记——(3)软件流程与代码
[复制链接]
1 软件开发设计
1.1. 软件流程图
根据功能需求,编制软件流程如下:
图 4 1 自行车风火轮软件流程图
上述流程中,初始化部分由main函数完成。之后main函数进入idle。
由Timer01中断记录车轮旋转一周的时间T,并将该时间T/2n得到每一个等分间隔的时间,该时间为Timer00的中断。
在每次Timer00中断将LED灯条按照存储的图像数据逐个控制LED的亮度和颜色。
图 4 2 软件代码执行时间片示意图
1.2. 代码
1.2.1 Main函数- void main(void)
- {
- UINT8 k;
- unsigned char mode;
-
- mode=0;
-
- init_io();
-
- init_timer();
-
- /* Enable Interrupt */
- EI();
- /* Start Timer, i.e. PWM output */
- start_timer();
- while(1){
- if (P13.7 == 0)
- {
- if (mode)
- {
- mode = 0;
- stop_timer();
- }
- }
- else
- {
- if (mode == 0)
- {
- mode = 1;
- start_timer();
- }
- }
- }
- }
复制代码
1.2.2 Timer01中断函数- __interrupt void interrupt_inttm01(){
- unsigned long div, count ;
- int h;
- if(isFirst){ isFirst = 0;return;}
- count = (unsigned)TDR01L + (unsigned)TDR01H * 256 ;
- /*TODO: OVF should be counted */
- //count = 0x10000 *
-
- div = count / 72;
- h = div / 256;
- TDR00H = h;
- TDR00L = div - h * 256;
- // the end
- }
复制代码
1.2.3 Timer00中断函数
- __interrupt void interrupt_inttm00(){
- char temp;
- UINT8 i, r=0,g=0,b=0;
- preamble();
- if( index >= LEDCOUNT*2-2){
- index = 0;
- }
- for(i=0;i<LEDCOUNT;i++){
- temp = figure[index][i];
- r = (temp & 0x4)<< 5 >> 7;
- g = (temp & 0x2) << 6 >> 7;
- b = (temp & 0x1) << 7 >> 7;
- light_single_led(r,g,b);
- }
- postfix();
- index ++;
- }// the end
复制代码
1.2.4 LED灯带控制函数在每个dT=T/2n的时间间隔内,输出控制LED灯条的颜色亮度数据。时序波形按照LPD6803的要求生成。其中DLCK为P00,DIN为P01,采用GPIO生成时序波形图。
图 4 3 LPD6803时序波形
- const static UINT8 figure[][LEDCOUNT+1] = {
- { 0x04 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 } ,
- { 0x02 , 0x04 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 } ,
- { 0x01 , 0x02 , 0x04 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 } ,
- { 0x04 , 0x01 , 0x02 , 0x04 , 0x00 , 0x00 , 0x00 , 0x00 } ,
- { 0x02 , 0x04 , 0x01 , 0x02 , 0x04 , 0x00 , 0x00 , 0x00 } ,
- { 0x01 , 0x02 , 0x04 , 0x01 , 0x02 , 0x04 , 0x00 , 0x00 } ,
- { 0x00 , 0x01 , 0x02 , 0x04 , 0x01 , 0x02 , 0x04 , 0x00 } ,
- { 0x01 , 0x02 , 0x04 , 0x01 , 0x02 , 0x04 , 0x00 , 0x00 } ,
- { 0x02 , 0x04 , 0x01 , 0x02 , 0x04 , 0x00 , 0x00 , 0x00 } ,
- { 0x04 , 0x01 , 0x02 , 0x04 , 0x00 , 0x00 , 0x00 , 0x00 } ,
- { 0x01 , 0x02 , 0x04 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 } ,
- { 0x02 , 0x04 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 } ,
- { 0x04 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 } };
- //long count;
- UINT8 status ;
- UINT8 isFirst;
- UINT8 index ;
- void clk(void){
- P0 &= ~CLK_PIN_BIT;
- P0 |= CLK_PIN_BIT;
- }
-
- void write_bit(UINT8 data){
- //P0 |= (0x1&data) << DAT_PIN ;
- //clk();
- P0 = ( (0x1&data) << DAT_PIN ) & ~CLK_PIN_BIT ;
- P0 |= CLK_PIN_BIT;
- //P0 = (data<<4) ;
- //P0 = 0x08 |(data<<4) ;
- }
-
- void write_bits(UINT8 data, UINT8 bitcount){
- /* bitcount should be less than 8*/
- UINT8 tmp, i=0, k=(0x01<<(bitcount-1));
-
- for(i=1;i<=bitcount;i++){
- tmp = (data & k)>>(bitcount-i);
- write_bit(tmp);
- k=k>>1;
- }
- }
- void preamble(void){
- UINT8 i;
- for(i=0;i<32;i++){
- write_bit(0);
- }
- }
- void postfix(void){
- int i;
- for(i=0;i<LEDCOUNT;i++){
- write_bit(0);
- }
- }
- void write_single_channel(UINT8 ch){
- // UINT8 tp = ch & 0x1f;
- write_bits( ch & 0x1f, 5); // ignore the first 3 bits
- }
- void light_single_led(UINT8 r, UINT8 g, UINT8 b){
- write_bit(0x01);
- write_single_channel(r);
- write_single_channel(g);
- write_single_channel(b);
- }
【瑞萨电子MCU套件免费试用】自行车炫彩风火轮开发应用笔记——(3)软件流程与代码.pdf
(222.03 KB, 下载次数: 48)
-
复制代码
|
|