刚点亮时为红色,随后以0.5s为中断间隔蓝绿等颜色不停变换。
#include "derivative.h" /* include peripheral declarations */
#define BIT(x) (1 << x)
static int times = 0;
unsigned char bMode=0;
void led4_init(void)
{
SIM_SCGC5 |= BIT(10)+BIT(11); //enable portc clk gating
PORTC_PCR3 |= BIT(8); //config GPIO
PORTC_PCR4 |= BIT(8); //config GPIO
PORTC_PCR5 |= BIT(8); //config GPIO
PORTB_PCR16|=BIT(8);//config GPIO
PORTB_PCR17|=BIT(8);//config GPIO
PORTB_PCR18|=BIT(8);//config GPIO
PORTB_PCR19|=BIT(8);//config GPIO
PORTC_PCR0|=BIT(8);//config GPIO
PORTC_PCR2|=BIT(8);//config GPIO
GPIOB_PDDR =0x0f0000;
GPIOC_PDDR =0x003d;//配置PTC0、PTC4为输出
GPIOB_PDOR=0x090000;
GPIOC_PDOR=0x0008;
//GPIOC_PDDR |= (BIT(3)|BIT(4)|BIT(5)); //direction as output
//GPIOC_PDOR |= BIT(4); //green
}
void systick_init(void)
{
SYST_RVR = 10485000; //0.5s
SYST_CVR = 0;
SYST_CSR |= BIT(0) | BIT(1) | BIT(2);
}
void SysTick_Handler(void)
{
int i;
if(times % 2 == 0)
{
bMode=~bMode;
if(bMode)
{
GPIOB_PDOR=0x020000;
GPIOC_PDOR=0x0011;
}
else
{
GPIOB_PDOR=0x040000;
GPIOC_PDOR=0x0024;
}
}
times++;
i = SYST_CSR;
}
void Enable_Irq(void)
{
asm("CPSIE i");
}
int main(void)
{
systick_init();
led4_init();
Enable_Irq();
while(1);
return 0;
}
LED初始化
void led4_init(void)
{
SIM_SCGC5 |= BIT(10)+BIT(11); //enable portc clk gating
PORTC_PCR3 |= BIT(8); //config GPIO
PORTC_PCR4 |= BIT(8); //config GPIO
PORTC_PCR5 |= BIT(8); //config GPIO
PORTB_PCR16|=BIT(8);//config GPIO
PORTB_PCR17|=BIT(8);//config GPIO
PORTB_PCR18|=BIT(8);//config GPIO
PORTB_PCR19|=BIT(8);//config GPIO
PORTC_PCR0|=BIT(8);//config GPIO
PORTC_PCR2|=BIT(8);//config GPIO
GPIOB_PDDR =BIT(16)|BIT(17)|BIT(18)|BIT(19);
GPIOC_PDDR =BIT(0)|BIT(2)|BIT(3)|BIT(4)|BIT(5);//配置PTC0、PTC4为输出
GPIOB_PDOR=BIT(16)|BIT(19);
GPIOC_PDOR=BIT(3);
}
1SIM_SCGC5的10和11分别是PTB和PTC时钟打开接口
2BIT(8)代表对应PORTN_PCR控制器都为IO接口
3在这里B打开16 17 18 19,C打开0 2 3 4 5
4在这里输出红灯为PTB 16 19,PTC3
时钟中断初始化
void systick_init(void)
{
SYST_RVR = 10485000; //0.5s
SYST_CVR = 0;
SYST_CSR |= BIT(0) | BIT(1) | BIT(2);
}
在这里RVR决定可周期的值,一共只有24位,由于我们的频率是20.9MHZ,所以我们直接设定10485000,为一半,故而周期是0.5s,CVR为现在值设定为0,同时读取会使标志位清0,所以后续写完中断函数要进行清0
CSR为状态寄存器可以了解将0,1,2置1
void Enable_Irq(void)
{
asm("CPSIE i");
}
打开全局中断
时间中断函数
void SysTick_Handler(void)
{
int i;
if(times % 2 == 0)
{
bMode=~bMode;
if(bMode)
{
GPIOB_PDOR=0x020000;
GPIOC_PDOR=0x0011;
}
else
{
GPIOB_PDOR=0x040000;
GPIOC_PDOR=0x0024;
}
}
times++;
i = SYST_CSR;
}
读取CSR可以清楚标志位,所以设定变量来读取CSR。
在这里设定变量time来进行颜色转换,每次时间周期到了进行转换
这里函数名需要注意,需要和NVIC中一致
具体由codewarrior自己生成,可以选择用一样函数名,也可以将这个c文件中的函数名进行修改