危機處理-不知道該怎麼說的TIMER
究竟是道德的淪喪還是人性的崩壞使我們要用內部中斷去做timer呢
同學,請讓我們繼續看下去.......
因為這次題目會用到LCD相關的控制
所以相關的Source File 都要給他催落
1. LCD_Driver.c
2. LCD_Driver_Extend.c
3. LCD_Point_Picture.C
其中 LCD_Driver_Extend.c新增了16*16的"電"字圖形
/*圖形 電2*/
unsigned char cyee[16][16]={
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0},
{1,0,1,1,1,1,0,1,0,1,1,1,1,0,1,0},
{1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0},
{1,0,1,1,1,1,0,1,0,1,1,1,1,0,1,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0},
{0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0},
{0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0},
{0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0},
};
還有在LCD_Driver_Extend.c新增了draw_cyee函式
//繪圖函式16X16像素 " 電 "
//圖片陣列,X座標,Y座標
void draw_cyee(unsigned char *buffer, unsigned char *pic_buffer, int x, int y ){
clr_part_pannal(buffer, 0, 128, 0, 7);
create_picture(buffer, 16*x, 16*y, pic_buffer, 16, 16);
draw_part_LCD(buffer, 0, 128, 0, 7);
}
主程式當中記得宣告會用到的陣列跟功能
//圖片陣列宣告
extern unsigned char cyee[16][16];
//畫布陣列宣告
unsigned char DisplayBuf [8*128];
//螢幕初始+背光宣告
Initial_pannel(); //call initial pannel function
clr_all_pannal();
DrvGPIO_Open(E_GPD, 14, E_IO_OUTPUT);
DrvGPIO_ClrBit(E_GPD,14);
主菜:
我保留了幾乎大部分範例的原始碼所以不是每一個都有用到
要注意的是透過調整TCMPR值去改變頻率(控制停留秒數)
#include
#include "NUC1xx.h"
#include "Driver\DrvGPIO.h"
#include "Driver\DrvSYS.h"
#include "Scankey.h"
#include "Seven_Segment.h"
#include "LCD_Driver.h"
#include "LCD_Driver_Extend.h"
extern unsigned char Ascii[];
extern unsigned char cyee[16][16];
unsigned char DisplayBuf [8*128];
void Step_Motor(char cnt);
void Init_GPIO();
void Init_7_Segment(void);
void TIMER0_Init(void);
void TMR0_IRQHandler(void);
void TIMER1_Init(void);
void TMR1_IRQHandler(void);
int i, seg_cnt=0, step_cnt;
int frequency=100;
int a=0, b=0, move_cnt=0;
int main (void)
{
UNLOCKREG();
DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1);
LOCKREG();
/* HCLK clock source. 0: external 12MHz; 4:internal 22MHz RC oscillator */
DrvSYS_SelectHCLKSource(0);
LOCKREG();
Initial_pannel(); //call initial pannel function
clr_all_pannal();
DrvGPIO_Open(E_GPD, 14, E_IO_OUTPUT);
DrvGPIO_ClrBit(E_GPD,14);
Init_GPIO(); //GPC12-15
OpenKeyPad();
Init_7_Segment();
TIMER0_Init(); //keypad, 7-segment
TIMER1_Init(); //step motor
draw_cyee(DisplayBuf, cyee, a, b);
while(1);
}
void TMR0_IRQHandler()
{
TIMER0->TISR.TIF = 1; // clear Interrupt flag
//keypad control
i=Scankey();
if(i!=0){
switch(i)
{
case 1:
TIMER1->TCMPR = 1000000; //1Hz
//frequency=100;
break;
case 2:
TIMER1->TCMPR = 500000; //2Hz
//frequency=400;
break;
case 3:
TIMER1->TCMPR = 250000; //4Hz
//frequency=800;
break;
}
}
}
void TIMER0_Init(void)
{
/* Step 1. Enable and Select Timer clock source */
SYSCLK->CLKSEL1.TMR0_S = 0; //Select 12Mhz for Timer0 clock source
SYSCLK->APBCLK.TMR0_EN = 1; //Enable Timer0 clock source
/* Step 2. Select Operation mode */
TIMER0->TCSR.MODE = 1; //Select operation mode
/* Step 3. Select Time out period = (Period of timer clock input) * (8-bit Prescale + 1) * (24-bit TCMP)*/
TIMER0->TCSR.PRESCALE = 11; // Set Prescale [0~255]
TIMER0->TCMPR = 5000; // Set TCMPR [0~16777215]
//Timeout period = (1 / 12MHz) * ( 11 + 1) * 5000 = 0.005 sec
/* Step 4. Enable interrupt */
TIMER0->TCSR.IE = 1;
TIMER0->TISR.TIF = 1; //Write 1 to clear for safty
NVIC_EnableIRQ(TMR0_IRQn); //Enable Timer0 Interrupt
/* Step 5. Enable Timer module */
TIMER0->TCSR.CRST = 1; //Reset up counter
TIMER0->TCSR.CEN = 1; //Enable Timer0
}
void TMR1_IRQHandler()
{
TIMER1->TISR.TIF = 1; // clear Interrupt flag
step_cnt++;
if(step_cnt>=8) step_cnt=0;
Step_Motor(step_cnt);
//控制字母前進規則
a=a+1;
if(a>=8){
a=0;
b=b+1;
if(b>=4)b=0;
}
draw_cyee(DisplayBuf, cyee, a, b);
}
void TIMER1_Init(void)
{
/* Step 1. Enable and Select Timer clock source */
SYSCLK->CLKSEL1.TMR1_S = 0; //Select 12Mhz for Timer1 clock source
SYSCLK->APBCLK.TMR1_EN = 1; //Enable Timer1 clock source
/* Step 2. Select Operation mode */
TIMER1->TCSR.MODE = 1; //Select operation mode
/* Step 3. Select Time out period = (Period of timer clock input) * (8-bit Prescale + 1) * (24-bit TCMP)*/
TIMER1->TCSR.PRESCALE = 11; // Set Prescale [0~255]
TIMER1->TCMPR = 1000000; // Set TCMPR [0~16777215]
//Timeout period = (1 / 12MHz) * ( 11 + 1) * 1000000 = 1 sec (1Hz)
/* Step 4. Enable interrupt */
TIMER1->TCSR.IE = 1;
TIMER1->TISR.TIF = 1; //Write 1 to clear for safty
NVIC_EnableIRQ(TMR1_IRQn); //Enable Timer0 Interrupt
/* Step 5. Enable Timer module */
TIMER1->TCSR.CRST = 1; //Reset up counter
TIMER1->TCSR.CEN = 1; //Enable Timer0
}
void Step_Motor(char cnt)
{
switch (cnt)
{
case 0: //1000
DrvGPIO_SetBit(E_GPC,12);
DrvGPIO_ClrBit(E_GPC,13);
DrvGPIO_ClrBit(E_GPC,14);
DrvGPIO_ClrBit(E_GPC,15);
break;
case 1: //1100
DrvGPIO_SetBit(E_GPC,12);
DrvGPIO_SetBit(E_GPC,13);
DrvGPIO_ClrBit(E_GPC,14);
DrvGPIO_ClrBit(E_GPC,15);
break;
case 2: //0100
DrvGPIO_ClrBit(E_GPC,12);
DrvGPIO_SetBit(E_GPC,13);
DrvGPIO_ClrBit(E_GPC,14);
DrvGPIO_ClrBit(E_GPC,15);
break;
case 3: //0110
DrvGPIO_ClrBit(E_GPC,12);
DrvGPIO_SetBit(E_GPC,13);
DrvGPIO_SetBit(E_GPC,14);
DrvGPIO_ClrBit(E_GPC,15);
break;
case 4: //0010
DrvGPIO_ClrBit(E_GPC,12);
DrvGPIO_ClrBit(E_GPC,13);
DrvGPIO_SetBit(E_GPC,14);
DrvGPIO_ClrBit(E_GPC,15);
break;
case 5: //0011
DrvGPIO_ClrBit(E_GPC,12);
DrvGPIO_ClrBit(E_GPC,13);
DrvGPIO_SetBit(E_GPC,14);
DrvGPIO_SetBit(E_GPC,15);
break;
case 6: //0001
DrvGPIO_ClrBit(E_GPC,12);
DrvGPIO_ClrBit(E_GPC,13);
DrvGPIO_ClrBit(E_GPC,14);
DrvGPIO_SetBit(E_GPC,15);
break;
case 7: //1001
DrvGPIO_SetBit(E_GPC,12);
DrvGPIO_ClrBit(E_GPC,13);
DrvGPIO_ClrBit(E_GPC,14);
DrvGPIO_SetBit(E_GPC,15);
break;
}
}
void Init_GPIO()
{
for(int i = 12 ; i < 16 ; i++)
DrvGPIO_Open(E_GPC, i, E_IO_OUTPUT);
}
void Init_7_Segment(void)
{
//Initial GPIOE [7:0] to output mode for 7-seg.
DrvGPIO_Open(E_GPE, 0, E_IO_OUTPUT);
DrvGPIO_Open(E_GPE, 1, E_IO_OUTPUT);
DrvGPIO_Open(E_GPE, 2, E_IO_OUTPUT);
DrvGPIO_Open(E_GPE, 3, E_IO_OUTPUT);
DrvGPIO_Open(E_GPE, 4, E_IO_OUTPUT);
DrvGPIO_Open(E_GPE, 5, E_IO_OUTPUT);
DrvGPIO_Open(E_GPE, 6, E_IO_OUTPUT);
DrvGPIO_Open(E_GPE, 7, E_IO_OUTPUT);
//Initial GPIOC [7:4] to output mode for nth 7-seg.
DrvGPIO_Open(E_GPC, 4, E_IO_OUTPUT);
DrvGPIO_Open(E_GPC, 5, E_IO_OUTPUT);
DrvGPIO_Open(E_GPC, 6, E_IO_OUTPUT);
DrvGPIO_Open(E_GPC, 7, E_IO_OUTPUT);
}
附上壓縮檔
點我
結束這回合
留言
張貼留言