嵌入式 综合应用实验报告 1.docx
《嵌入式 综合应用实验报告 1.docx》由会员分享,可在线阅读,更多相关《嵌入式 综合应用实验报告 1.docx(13页珍藏版)》请在冰豆网上搜索。
嵌入式综合应用实验报告1
《嵌入式系统与接口技术实验》
综合设计报告
班级13物联网
姓名黄梓恩
学号1308040106
指导教师沈兰
【综合应用实验目的】
【综合应用实验题目】
利用GEC2410实验板,设计并实现一个综合应用系统
【综合应用实验要求】
要用到已学过的所有GEC2410模块,包括:
●GPIO端口控制:
LED灯模块
●中断控制:
按键外部中断,内部中断(TimerorTicktime)
●UART串口:
键盘输入,显示器输出
●PWM定时器:
定时中断,tout0输出驱动蜂鸣器
●蜂鸣器:
●实时时钟:
当前时间,闹钟,节拍中断
●看门狗定时器:
WTD中断,Reset
【相关理论知识】
【实现方案】
主函数
#include"2410addr.h"
#include"2410lib.h"
#include"def.h"
externvoidwatchdog_test(void);
externvoid__irqwatchdog_int(void);
externvoid__irqTick_Isr(void);
externvoidOpenRtc(void);
externvoidBUZZER_PWM_Test(void);
externvoidEint_Init(void);
externvoidEnable_Eint(void);
externvoidwatchdog_test(void);
externchart;
intMain(void)
{chara,b,c;
ChangeClockDivider(1,1);
ChangeMPllValue(0xa1,0x3,0x1);
Port_Init();
Uart_Select(0);
Uart_Init(0,115200);
Eint_Init();//外部中断初始化
OpenRtc();//打开实时时钟节拍中断
watchdog_test();
Uart_Printf("**********HELLO**********\n");
Uart_Printf("1.PlanowhitLED\n");
Uart_Printf("2.Piano2\n");
DisableIrq(BIT_WDT);
DisableIrq(BIT_EINT0);
DisableIrq(BIT_EINT2);
DisableIrq(BIT_EINT8_23);//关这4个中断,为了在键盘没按'1'或'3'之前拒绝产生这4种中断
Uart_Printf("\n1.2.3\n");
//1是键盘电子琴,2是开发板按键电子琴,3跑马灯
while
(1)
{
a=Uart_Getch();Delay(300);
if(a=='1')
{
Uart_Printf("ESCbacktomenu\n");
BUZZER_PWM_Test();
}
if(a=='2')
{
Uart_Printf("ESCbacktomenu\n");
Enable_Eint();
{
b=Uart_Getch();Delay(300);
if(b==ESC_KEY)break;
}
}
else
Uart_Printf("Error!
pleasetryagain");
}
}
#include"2410addr.h"
#include"2410lib.h"
#include"def.h"
//使用K1按键触发EINT0,EINT0->GPF0
//使用K3按键触发EINT2,EINT2->GPF2
//使用K5按键触发EINT11,EINT11->GPG3
//使用K7按键触发EINT19,EINT19->GPG11
//KSCAN0->GPE11
intk=1;
chart;
char*week[8]={"","SUN","MON","TUES","WED","THURS","FRI","SAT"};
intyear,month,date,weekday,hour,min,sec;
void__irqwatchdog_int(void);
void__irqTick_Isr(void);
voidGet_Rtc(void);
voidBuzzer_Freq_Set(U32freq);
voidBuzzer_Stop(void);
void__irqEint0_ISR(void);
void__irqEint2_ISR(void);
void__irqEint11_19_ISR(void);
voidwatchdog_test(void)
{
//initializeinterruptregisters允许WatchDog中断
ClearPending(BIT_WDT);
//往中断向量表填WatchDog中断向量
pISR_WDT=(unsigned)watchdog_int;
//Prescalervalue=100、clockdivisionfactor=128
//t_watchdog=1/[PCLK/(Prescalervalue+1)/Division_factor]=0.00025856
//disablewatchdog
rWTCON=((100<<8)|(3<<3));
//看门狗时钟周期T=WTCNT*t_watchdog=4S
//看门狗喂狗
rWTDAT=15000;
rWTCNT=15000;
rWTCON|=((1<<5)|(1<<2));//enableWatchdogtimerangwatchdoginterrupt
//开中断
EnableIrq(BIT_WDT);
}
/****watchdog_int****/
void__irqwatchdog_int(void)
{
//清除中断
rGPFDAT=rGPFDAT&~(1<<7);Delay(2000);rGPFDAT=rGPFDAT|(1<<7);
rGPFDAT=rGPFDAT&~(1<<6);Delay(2000);rGPFDAT=rGPFDAT|(1<<6);
rGPFDAT=rGPFDAT&~(1<<5);Delay(2000);rGPFDAT=rGPFDAT|(1<<5);
rGPFDAT=rGPFDAT&~(1<<4);Delay(2000);rGPFDAT=rGPFDAT|(1<<4);
}
void__irqTick_Isr(void)
{k=-k;
if(k==1)
rGPFDAT=rGPFDAT&~(15<<4);
if(k==-1)
rGPFDAT=rGPFDAT|(15<<4);
//Get_Rtc();
//Uart_Printf("RTCTIME:
%4x-%02x-%02x-%s-%02x:
%02x:
%02x\n",year,month,date,week[weekday],hour,min,sec);
ClearPending(BIT_TICK);
}
voidOpenRtc(void)
{
pISR_TICK=(unsigned)Tick_Isr;
rTICNT=0xBF;//Ticktimeinterruptenable;Ticktimecountvalue=63
EnableIrq(BIT_TICK);//openRTCTICKINTERRUPT
}
voidGet_Rtc(void)
{
rRTCCON=0x01;//RTC读写使能,选择BCD时钟、计数器,无复位,1/32768
if(rBCDYEAR==0x99)
year=0x1999;
else
year=0x2000+rBCDYEAR;
month=rBCDMON;
date=rBCDDATE;
weekday=rBCDDAY;
hour=rBCDHOUR;
min=rBCDMIN;
sec=rBCDSEC;
rRTCCON=0x0;//RTC读写禁止,选择BCD时钟、计数器,无复位,1/32768
}
voidBUZZER_PWM_Test(void)
{
//U16freq;
//Uart_Printf("\nKEYCONTROLBUZZER\n");
//Uart_Printf("Press'ESC'keytoExitthisprogram!
\n\n");
while
(1)
{
U8key=Uart_Getch();//通过异步串行通道Uart输入按键值
switch(key)
{
case'1':
Buzzer_Freq_Set(200);rGPFDAT=rGPFDAT&~(1<<4);Delay(800);rGPFDAT=rGPFDAT|(1<<4);Buzzer_Stop();break;
case'2':
Buzzer_Freq_Set(300);rGPFDAT=rGPFDAT&~(1<<5);Delay(800);rGPFDAT=rGPFDAT|(1<<5);Buzzer_Stop();break;
case'3':
Buzzer_Freq_Set(500);rGPFDAT=rGPFDAT&~(1<<6);Delay(800);rGPFDAT=rGPFDAT|(1<<6);Buzzer_Stop();break;
case'4':
Buzzer_Freq_Set(800);rGPFDAT=rGPFDAT&~(1<<7);Delay(800);rGPFDAT=rGPFDAT|(1<<7);Buzzer_Stop();break;
case'5':
Buzzer_Freq_Set(1000);rGPFDAT=rGPFDAT&~(1<<4);Delay(800);rGPFDAT=rGPFDAT|(1<<4);Buzzer_Stop();break;
case'6':
Buzzer_Freq_Set(1200);rGPFDAT=rGPFDAT&~(1<<5);Delay(800);rGPFDAT=rGPFDAT|(1<<5);Buzzer_Stop();break;
case'7':
Buzzer_Freq_Set(1400);rGPFDAT=rGPFDAT&~(1<<6);Delay(200);rGPFDAT=rGPFDAT|(1<<6);Buzzer_Stop();break;
}
//Buzzer_Freq_Set(freq);
//Uart_Printf("\tFreq=%d\n",freq);//通过异步串行通道Uart输出频率
if(key==ESC_KEY)
{
Buzzer_Stop();//停止蜂鸣器
//{break;}
//Uart_Printf("\nChoiceagain\n");
return;
}
}
}
voidBuzzer_Freq_Set(U32freq)
{
rGPBCON=rGPBCON&~(3<<0)|(1<<1);//setGPB0astout0,pwmoutput
rTCFG0=rTCFG0&~0xff|15;//prescaler=15
rTCFG1=rTCFG1&~0xf|2;//divider=1/8
rTCNTB0=(PCLK>>7)/freq;//rTCNTB0=PCLK/{(prescaler+1)*divider*freq}
rTCMPB0=rTCNTB0>>1;//占空比50%
//disabledeadzone,auto-reload,inv-off,updateTCNTB0&TCMPB0,starttimer0
rTCON=rTCON&~0x1f|(0<<4)|(1<<3)|(0<<2)|(1<<1)|
(1);
rTCON&=~(1<<1);//clearmanualupdatebit
}
voidBuzzer_Stop(void)
{
rGPBCON|=1;
rGPBCON=rGPBCON&~3|1;//setGPB0asoutput
rGPBDAT&=~1;//output0
}
/***********外部中断初始化************/
voidEint_Init(void)
{
//设置GPE11为输出0
rGPECON=(rGPECON|(3<<22))&~(2<<22);
rGPEUP|=(1<<11);
rGPEDAT&=~(1<<11);
rGPFCON=rGPFCON&~(3)|(1<<1);//GPF0设置为EINT0
rGPFCON=rGPFCON&~(3<<4)|(1<<5);//GPF2设置为EINT2
rGPGCON=rGPGCON&~(3<<6)|(1<<7);//GPG3设置为EINT11
rGPGCON=rGPGCON&~(3<<22)|(1<<23);//GPG11设置为EINT19
rGPFUP|=(1<<0);//disableGPF0pullup
rGPFUP|=(1<<2);//disableGPF2pullup
rGPGUP|=(1<<3);//disableGPG3pullup
rGPGUP|=(1<<11);//disableGPG11pullup
rEXTINT0=(rEXTINT0&~(7<<0))|(2<<0);//EINT0->fallingedgetriggered
rEXTINT0=(rEXTINT0&~(7<<8))|(2<<8);//EINT2->fallingedgetriggered
rEXTINT1=(rEXTINT1&~(7<<12))|(0x2<<12);//EINT11->fallingedgetriggered
rEXTINT2=(rEXTINT2&~(7<<12))|(0x2<<12);//EINT19->fallingedgetriggered
pISR_EINT0=(unsigned)Eint0_ISR;
pISR_EINT2=(unsigned)Eint2_ISR;
pISR_EINT8_23=(unsigned)Eint11_19_ISR;
}
/***********开外部中断************/
voidEnable_Eint(void)
{
rEINTPEND=0xffffff;//toclearthepreviouspendingstates
rSRCPND|=BIT_EINT0|BIT_EINT2|BIT_EINT8_23;
rINTPND|=BIT_EINT0|BIT_EINT2|BIT_EINT8_23;
rEINTMASK=~((1<<11)|(1<<19));
//rINTMSK=~(BIT_EINT0|BIT_EINT2|BIT_EINT8_23);
EnableIrq(BIT_EINT0);
EnableIrq(BIT_EINT2);
EnableIrq(BIT_EINT8_23);
}
/***********外部中断服务程序************/
void__irqEint0_ISR(void)
{
ClearPending(BIT_EINT0);
Buzzer_Freq_Set(300);//修改PMW频率
Delay(800);Buzzer_Stop();
Uart_Printf("Freq=300\n");//通过异步串行通道Uart输出频率
}
void__irqEint2_ISR(void)
{
ClearPending(BIT_EINT2);
Buzzer_Freq_Set(500);//修改PMW频率
Delay(800);Buzzer_Stop();
Uart_Printf("Freq=500\n");//通过异步串行通道Uart输出频率
}
void__irqEint11_19_ISR(void)
{
if(rEINTPEND&(1<<11))
{
Buzzer_Freq_Set(800);//修改PMW频率
Delay(800);Buzzer_Stop();
Uart_Printf("Freq=800\n");//通过异步串行通道Uart输出频率
rEINTPEND=(1<<11);//清除子登记,方便下次中断,方法比较特别,在对应为写1将它清为0
ClearPending(BIT_EINT8_23);
}
elseif(rEINTPEND&(1<<19))
{
Buzzer_Freq_Set(1000);//修改PMW频率
Delay(800);Buzzer_Stop();
Uart_Printf("Freq=1000\n");//通过异步串行通道Uart输出频率
rEINTPEND=(1<<19);
ClearPending(BIT_EINT8_23);
}
else
{
Uart_Printf("othersENTsareoccurred\n");
rEINTPEND=0xffffff;
ClearPending(BIT_EINT8_23);
}
}
【实现过程及结果】
开始菜单,选择1,为键盘电子琴,不同按键会有不同的灯亮起
按ESC键返回选择菜单,若按了错误按键,系统提示错误,
选择2为开发板按键电子琴,会显示你所按按键的频率
【设计总结】
【参考文献】
1、
2、
3、