受当前疫情影响,目前只能在家办公,终于有时间可以玩BLAZAR-β学习板,现将该实验一整理如下,供大家参考
一 实验所需条件
1) BLAZAR-β学习板
2) Jlink调试器
3) codewarrior10.5软件开发环境
二 实验内容
任务1:两个灯同时点亮,同时熄灭,进行闪烁
分析:1) 选择板子上led4和led7两个LED灯,查看原理图可知,这两个LED控制管脚是连接PortC模块
2) 使能PortC模块时钟,配置相应的PortC为GPIO,再配置方向为output
3) 查看原理图可知,如果LED控制管脚输出高电平,则LED点亮,否则LED熄灭
实现代码:
void delay(void)
{
int i,j;
for(i=0;i<300;i++)
for(j=0;j<2000;j++)
;
}
void led4_init(void)
{
SIM_SCGC5 |= (1 << 11); //enable portc clk gating
PORTC_PCR3 |= (1 << 8); //config as GPIO
PORTC_PCR4 |= (1 << 8); //config as GPIO
PORTC_PCR5 |= (1 << 8); //config as GPIO
GPIOC_PDDR |= ((1 << 3)|(1 << 4)|(1 << 5)); //direction as output
}
void led7_init(void)
{
SIM_SCGC5 |= (1 << 11); //enable portc clk gating
SIM_SCGC5 |= (1 << 10); //enable portb clk gating
PORTC_PCR0 |= (1 << 8); //config as GPIO
PORTC_PCR2 |= (1 << 8); //config as GPIO
PORTB_PCR19 |= (1 << 8); //config as GPIO
GPIOC_PDDR |= ((1 << 0)|(1 << 2)); //direction as output
GPIOB_PDDR |= (1 << 19); //direction as output
}
void led4_and_led7_on(void)
{
GPIOC_PDOR |= (1 << 0); //output high Green
GPIOC_PDOR |= (1 << 4); //output high
delay();
GPIOC_PDOR &= ~(1 << 0);//output low
GPIOC_PDOR &= ~(1 << 4);//output low
delay();
}
任务2:两灯交替闪烁
分析:在实验一基础上,修改代码使其你亮我灭,你灭我亮即可
实现代码片段:
void led4_or_led7_on(void)
{
GPIOC_PDOR |= (1 << 0); //output high Green
GPIOC_PDOR &= ~(1 << 4);//output low
delay();
GPIOC_PDOR |= (1 << 4); //output high
GPIOC_PDOR &= ~(1 << 0);//output low
delay();
}
任务3:将两个LED灯状态与两个按键相关联,即当按键按下时,其中一个LED亮,松开按键时,该LED灭
分析:1) 选择S4,S2按键,查看原理图可知,S4连接到PortC9管脚,S2连接到PortC7管脚
2) 配置PortC9和PortC7管脚为input方向,然后读取PDIR寄存器来获取按键状态
3) 为了保证按键松开后,PortC9和PortC7管脚状态确定,所以需要使能内部上拉电阻
实现代码片段:
void s4_init(void)
{
SIM_SCGC5 |= (1 << 11); //enable portc clk gating
PORTC_PCR9 |= ((1 << 8)|(1 << 1)|(1 << 0)); //config GPIO and enable pull-up
GPIOC_PDDR &= ~(1 << 9); //direction as input
}
void s2_init(void)
{
SIM_SCGC5 |= (1 << 11); //enable portc clk gating
PORTC_PCR7 |= ((1 << 8)|(1 << 1)|(1 << 0)); //config GPIO and enable pull-up
GPIOC_PDDR &= ~(1 << 7); //direction as input
}
void task3_implement(void)
{
if((GPIOC_PDIR & (1 << 9)) == 0)
{
GPIOC_PDOR |= (1 << 4); //output high
}
else if((GPIOC_PDIR & (1 << 7)) == 0)
{
GPIOC_PDOR |= (1 << 0); //output high
}
else {
GPIOC_PDOR &= ~(1 << 4);//output low
GPIOC_PDOR &= ~(1 << 0);//output low
}
}
任务4:同任务3,略
任务5:通过按键改变两灯闪烁模式,比如刚开始两灯同时闪烁,当按键按下后,两灯变交替闪烁,再按下按键,两灯变同时闪烁
分析:1) 选择S4按键
2) 需要一个全局变量记录按键信息
实现代码片段:
void task5_implement(void)
{
if((GPIOC_PDIR & (1 << 9)) == 0)
mode = !mode;
switch(mode)
{
case 1:
led4_or_led7_on();
break;
default:
led4_and_led7_on();
break;
}
}
至此,实验一就完成啦,欢迎大家批评指正,谢谢^_^ |