《矿大飞思卡尔实验指导书及程序.doc》由会员分享,可在线阅读,更多相关《矿大飞思卡尔实验指导书及程序.doc(20页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、中国矿业大学信电学院飞思卡尔实验指导书及实验程序实验一(4学时)一、实验题目实验硬件与软件认识二、实验目的与要求1. 正确认识硬件系统2. 掌握IAR软件的基本使用方法三、主要实验内容1. 硬件评估板认识与MCU的外观识别a. 根据原理图找到不同器件与MCU相连接的管脚b. 理解系统的基本稳压电路2. IAR安装与基本使用3. 评估板升级与例程下载四、检验方式1. 结合原理图找到不同器件和MCU的连接管脚2. 成功下载例程,并能简单演示。即随着手势的变化,三色LED呈现不同状态。实验二(4学时)一、实验题目LED发光与变色实验二、实验目的与要求1. 掌握I/O口的基本编程方法2. 掌握基本工程
2、构建方法三、主要实验内容1. 开关控制LED及其编程方法a. 找到LED对应MCU的管脚b. 编程控制三个不同颜色LED亮灭2. 程序修改与调试四、检验方式1. 控制单个LED闪烁,要求能编程控制闪烁频率2. 控制三个LED交叉闪烁,要求能编程控制闪烁的顺序和频率实验三(4学时)一、实验题目串行通信实验二、实验目的与要求1. 掌握MCU串行通信基本编程方法2. 学会MCU与微机的串行通信编程三、主要实验内容1. 轮询方式串口通信2. 中断方式串口通信3. 程序修改与调试四、验证方式1. 连接上位机,从串口以不同波特率发送字符,并得到下位机的反馈实验四(4学时)一、实验题目串口控制LED实验二、
3、实验目的与要求1. 熟练应用三色LED控制2. 熟练应用串口通信三、主要实验内容1. 利用串口控制LED显示赤橙黄绿青蓝紫七色2. 程序修改与调试四、验证方式1. 演示,要求三色LED能从赤逐渐显示到紫实验五(8学时)一、实验题目加速度传感器读取二、实验目的与要求1. 认识I2C通信协议2. 利用I2C协议与传感器进行通信3. 熟练应用三色LED控制三、主要实验内容1. 读取加速度传感器三轴加速度和根据不同加速度改变LED颜色2. 程序修改与调试四、验证方式1. 从加速度传感器读取数据,并在串口打印输出2. 根据加速度传感器不同状态改变三色LED的颜色实验六(4学时)一、实验题目打飞机小游戏设
4、计二、实验目的与要求1. 加速度传感器熟练应用2. 熟练运用串口三、主要实验内容1. 综合利用多个构件读取加速度传感器数据控制游戏中的飞机运动2. 程序修改与调试四、验证方式完成游戏获得5000分实验七(4学时)一、实验题目简单计步器二、实验目的与要求1. 加速度传感器熟练应用2. 计步算法理解三、主要实验内容1. 综合利用多个构件读取加速度传感器数据完成简单人体计数2. 程序修改与调试四、验证方式1. 走10步、20步、30步,LED成不同颜色实验程序实验一 实验硬件与软件认识int main (void) #ifdef CMSIS / If we are conforming to CMS
5、IS, we need to call start here start();#endif printf(nrRunning the GGGGGG project.nr); SIM_SCGC5 |= SIM_SCGC5_PORTB_MASK; SIM_SCGC5 |= SIM_SCGC5_PORTD_MASK; PORTB_PCR19=PORT_PCR_MUX(1); PORTB_PCR18=PORT_PCR_MUX(1); PORTD_PCR1=PORT_PCR_MUX(1); GPIOB_PDDR|=119; GPIOB_PDDR|=118; GPIOD_PDDR|=11; GPIOB_P
6、DOR|=(119); GPIOD_PDOR|=(11);while(1) GPIOB_PDOR&=(118); for(uint32 i=0;i0x4fffff;i+); GPIOB_PDOR|=(118); for(uint32 i=0;i0x4fffff;i+); int main (void) #ifdef CMSIS / If we are conforming to CMSIS, we need to call start here start();#endif printf(nrRunning the project.nr); SIM_SCGC5 |= SIM_SCGC5_POR
7、TB_MASK; SIM_SCGC5 |= SIM_SCGC5_PORTD_MASK; PORTB_PCR19=PORT_PCR_MUX(1); PORTB_PCR18=PORT_PCR_MUX(1); PORTD_PCR1=PORT_PCR_MUX(1); GPIOB_PDDR|=119; GPIOB_PDDR|=118; GPIOD_PDDR|=11;while(1)GPIOB_PDOR&=(118); GPIOB_PDOR|=(119); GPIOD_PDOR|=(11); for(uint32 i=0;i0x4fffff;i+); GPIOB_PDOR|=(118); GPIOB_PD
8、OR&=(119); GPIOD_PDOR|=(11); for(uint32 i=0;i0x4fffff;i+); GPIOB_PDOR|=(118); GPIOB_PDOR|=(119); GPIOD_PDOR&=(11); for(uint32 i=0;iSOPT2 |=SIM_SOPT2_UART0SRC(1); UART0_C2 &=(UART0_C2_TE_MASK | UART0_C2_RE_MASK); sbr = (uint16)(uart0_clk_khz*1000)/(baud_rate * 16); UART0_BDH=UART0_BDH &(UART0_BDH_SBR
9、_MASK); UART0_BDL=(uint8)(sbr & UART0_BDL_SBR_MASK); UART0_C4= UART0_C4_OSR(15); UART0_C1=0x00; UART0_S1 |=0x1F; UART0_S2 |=(UART0_S2_LBKDE_MASK|UART0_S2_RXEDGIF_MASK); UART0_C2 |=(UART_C2_TE_MASK|UART_C2_RE_MASK);int main (void) char ch;#ifdef CMSIS / If we are conforming to CMSIS, we need to call
10、start here start();#endif UART_Init(9600); printf(nrRunning the sixianghuibao project.nr);while(1)while (!(UART0_S1&UART0_S1_RDRF_MASK); ch=UART0_D; while (!(UART0_S1&UART0_S1_TDRE_MASK); UART0_D=ch; void UART_Init(uint32 baud_rate) uint16 uart0_clk_khz=24000; uint16 sbr; PORTA_PCR1=PORT_PCR_MUX(0x0
11、2); PORTA_PCR2=PORT_PCR_MUX(0x02); SIM_SCGC4|=SIM_SCGC4_UART0_MASK; SIM_SOPT2|=SIM_SOPT2_UART0SRC(1); UART0_C2 &=(UART0_C2_TE_MASK | UART0_C2_RE_MASK); sbr=(uint16)(uart0_clk_khz*1000)/(baud_rate*16); UART0_BDH=0x00; UART0_BDL=0x0D; UART0_C4=UART0_C4_OSR(15); UART0_C1=0x00; UART0_S1 |=0x1f; UART0_C2
12、 |=(UART_C2_TE_MASK | UART_C2_RE_MASK); void UART_irq_enable()UART0_C2|= UART0_C2_RIE_MASK;enable_irq(12);void uart0_isr() char ch; ch=UART0_D; while (!(UART0_S1 & UART0_S1_TDRE_MASK); UART0_D=ch;int main(void) char ch;#ifdef CMSIS start();#endif UART_Init (); UART_irq_enable(); printf(nrhollomotonr
13、);while(1)/*/Isr.h#ifndef _ISR_H#define _ISR_H 1/* Example */*#undef VECTOR_101#define VECTOR_101 lpt_isr/ ISR(s) are defined in your project directory.extern void lpt_isr(void);*/#undef VECTOR_028#define VECTOR_028 uart0_isrextern void uart0_isr(void);#endif /_ISR_H实验三 串行通信实验/* * File:i2c_accel.c *
14、 Purpose:Main process * */#include common.h#ifdef CMSIS#include start.h#endif#define SENSOR_I2C_ADDRESS 0x1D#define IICWRITE(iicaddress) (iicaddress1) & 0xFE)#define IICREAD(iicaddress) (iicaddress1) | 0x01)#define SENSOR_I2C_ADDRESS_W IICWRITE(SENSOR_I2C_ADDRESS)#define SENSOR_I2C_ADDRESS_R IICREAD
15、(SENSOR_I2C_ADDRESS)void i2c_Init() SIM_SCGC4 |= SIM_SCGC4_I2C0_MASK; SIM_SCGC5 |= SIM_SCGC5_PORTC_MASK; PORTE_PCR24 = PORT_PCR_MUX(5)| PORT_PCR_PE_MASK | PORT_PCR_PS_MASK; PORTE_PCR25 = PORT_PCR_MUX(5)| PORT_PCR_PE_MASK | PORT_PCR_PS_MASK; I2C0_F = 0x00; I2C0_C1 = I2C_C1_IICEN_MASK; I2C0_FLT |= I2C
16、_FLT_FLT(16); uint8 i2c_read_byte(uint8 reg) uint16 ch; uint8 c; I2C0_C1 |= I2C_C1_TX_MASK; I2C0_C1 &= I2C_C1_TXAK_MASK; I2C0_S |= I2C_S_IICIF_MASK; I2C0_C1 |= I2C_C1_MST_MASK; I2C0_D = SENSOR_I2C_ADDRESS_W; while(I2C0_S & I2C_S_IICIF_MASK) = 0); I2C0_S |= I2C_S_IICIF_MASK; while(I2C0_S & I2C_S_RXAK
17、_MASK) = 1); I2C0_D = reg; while(I2C0_S & I2C_S_IICIF_MASK) = 0); I2C0_S|=I2C_S_IICIF_MASK; while(I2C0_S & I2C_S_RXAK_MASK) = 1); I2C0_C1 |= I2C_C1_RSTA_MASK; for(ch = 0; ch 100; ch+) asm(nop); I2C0_D = SENSOR_I2C_ADDRESS_R; while(I2C0_S & I2C_S_IICIF_MASK) = 0); I2C0_S|=I2C_S_IICIF_MASK; I2C0_C1 &=
18、 I2C_C1_TX_MASK; I2C0_C1 |= I2C_C1_TXAK_MASK; c = I2C0_D; while(I2C0_S & I2C_S_IICIF_MASK) = 0); I2C0_S|=I2C_S_IICIF_MASK; I2C0_C1 &= I2C_C1_MST_MASK; c = I2C0_D; for(ch = 0; ch 500; ch+) asm(nop); return c;void i2c_write_byte(uint8 reg, uint8 m) uint16 ch; I2C0_C1 |= I2C_C1_TX_MASK; I2C0_C1 &= I2C_
19、C1_TXAK_MASK; I2C0_S |= I2C_S_IICIF_MASK; I2C0_C1 |= I2C_C1_MST_MASK; I2C0_D = SENSOR_I2C_ADDRESS_W; while(I2C0_S & I2C_S_IICIF_MASK) = 0); I2C0_S |= I2C_S_IICIF_MASK; while(I2C0_S & I2C_S_RXAK_MASK) = 1); I2C0_D = reg; while(I2C0_S & I2C_S_IICIF_MASK) = 0); I2C0_S|=I2C_S_IICIF_MASK; while(I2C0_S &
20、I2C_S_RXAK_MASK) = 1); I2C0_D = m; while(I2C0_S & I2C_S_IICIF_MASK) = 0); I2C0_S|=I2C_S_IICIF_MASK; while(I2C0_S & I2C_S_RXAK_MASK) = 1); I2C0_C1 &= I2C_C1_MST_MASK; for(ch = 0; ch SC = 0 | TPM_SC_CMOD(0); TPM0_BASE_PTR-CNT = 0; TPM0_BASE_PTR-MOD = period; TPM0_C1SC = 0 | TPM_CnSC_MSB_MASK | TPM_CnS
21、C_ELSB_MASK; TPM0_C1V = duty; TPM0_BASE_PTR-SC = 0 | TPM_SC_CMOD(1) |TPM_SC_PS(4) ; PORTB_PCR18 |= PORT_PCR_MUX(3); PORTB_PCR19 |= PORT_PCR_MUX(3); TPM2_BASE_PTR-SC = 0 | TPM_SC_CMOD(0); TPM2_BASE_PTR-CNT = 0; TPM2_BASE_PTR-MOD = period; TPM2_C0SC = 0 | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSB_MASK; TPM2_C
22、0V = duty; TPM2_C1SC = 0 | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSB_MASK; TPM2_C1V = duty; TPM2_BASE_PTR-SC = 0 | TPM_SC_CMOD(1) |TPM_SC_PS(4) ;void pwm_Set(uint8 ch, uint16 duty) switch (ch) case 1 : TPM0_C1V = duty; break; case 2 : TPM2_C0V = duty; break; case 3 : TPM2_C1V = duty; break; /*/uint8 x;uint8
23、 XH, XL, YH, YL, ZH, ZL;int8 xh, yh, zh;uint8 temp;/*/int main (void) #ifdef CMSIS / If we are conforming to CMSIS, we need to call start here start();#endif printf(nrRunning the experiment_5 project.nr); UART_Init(9600); i2c_Init(); pwm_Init(100, 50); x = i2c_read_byte(0x0D); if (x = 0x1A) i2c_writ
24、e_byte(0x2A, 0x19); i2c_write_byte(0x0E, 0x01); else printf(nrPlease check out your MMA81451!nr); while(1) x = i2c_read_byte(0x00); if (x & 0x08 = 0x08) XH = i2c_read_byte(0x01); XL = i2c_read_byte(0x02); YH = i2c_read_byte(0x03); YL = i2c_read_byte(0x04); ZH = i2c_read_byte(0x05); ZL = i2c_read_byt
25、e(0x06); uart0_putchar (UART0_BASE_PTR, 0xFF); uart0_putchar (UART0_BASE_PTR, XH - 1); uart0_putchar (UART0_BASE_PTR, XL); uart0_putchar (UART0_BASE_PTR, YH - 1); uart0_putchar (UART0_BASE_PTR, YL); uart0_putchar (UART0_BASE_PTR, ZH - 1); uart0_putchar (UART0_BASE_PTR, ZL); xh = i2c_read_byte(0x01);
26、 yh = i2c_read_byte(0x03); zh = i2c_read_byte(0x05); temp = (uint8)(128 + xh) / 256.0 * 100.0); pwm_Set(1,temp); temp = (uint8)(128 + yh) / 256.0 * 100.0); pwm_Set(2,temp); temp = (uint8)(128 + zh) / 256.0 * 100.0); pwm_Set(3,temp); else continue; /*/实验四 串口控制LED实验 .C#include common.h#ifdef CMSIS#inc
27、lude start.h#endif#define SENSOR_I2C_ADDRESS 0x1D#define IICWRITE(iicaddress) (iicaddress1) & 0xFE)#define IICREAD(iicaddress) (iicaddressSC=0|TPM_SC_CMOD(0);TPM0_BASE_PTR-CNT=0;TPM0_BASE_PTR-MOD=period;TPM0_C1SC=0|TPM_CnSC_MSB_MASK |TPM_CnSC_ELSB_MASK;TPM0_C1V=duty;TPM0_BASE_PTR-SC=0|TPM_SC_CMOD(1)
28、 |TPM_SC_PS(4); PORTB_PCR18|=PORT_PCR_MUX(3); PORTB_PCR19|=PORT_PCR_MUX(3);TPM2_BASE_PTR-SC=0|TPM_SC_CMOD(0);TPM2_BASE_PTR-CNT=0;TPM2_BASE_PTR-MOD=period;TPM2_C0SC=0|TPM_CnSC_MSB_MASK |TPM_CnSC_ELSB_MASK;TPM2_C0V=duty;TPM2_C1SC=0|TPM_CnSC_MSB_MASK |TPM_CnSC_ELSB_MASK;TPM2_C1V=duty;TPM2_BASE_PTR-SC=0
29、|TPM_SC_CMOD(1) |TPM_SC_PS(4);void pwm_Set(uint8 ch,uint16 duty) switch(ch) case 1:TPM0_C1V=duty; break; case 2:TPM2_C0V=duty; break; case 3:TPM2_C1V=duty; break; void uart0_isr() char ch; ch=UART0_D; switch(ch) case 1: pwm_Set(2,0); pwm_Set(1,100); pwm_Set(3,100); break; case 2: pwm_Set(2,0); pwm_S
30、et(3,50); pwm_Set(1,100); break; case 3: pwm_Set(2,0); pwm_Set(3,0); pwm_Set(1,100); break; case 4: pwm_Set(2,100); pwm_Set(1,100); pwm_Set(3,0); break; case 5: pwm_Set(2,100); pwm_Set(3,0); pwm_Set(1,0); break; case 6: pwm_Set(2,100); pwm_Set(3,100); pwm_Set(1,0); break; case 7: pwm_Set(2,50); pwm_
31、Set(3,100); pwm_Set(1,0); break; default: pwm_Set(2,100); pwm_Set(3,100); pwm_Set(1,100); break; while(!(UART0_S1 & UART0_S1_TDRE_MASK); UART0_D=ch;/*/int main(void)#ifdef CMSIS start();#endif UART_Init(9600); UART_irq_enable(); pwm_Init(100,100); while(1);/* while(1) pwm_Set(2,0); pwm_Set(1,100); pwm_Set(3,100); */实验四 串口控制LED实验.h#ifndef _ISR_H#define _ISR_H 1/* Example */*#undef VECTOR_101#define VECTOR_101 lpt_isr/ ISR(s) are defined in your project directory.extern void lpt_isr(void);*/#undef VECTOR_028#define VECTOR_028 uart0_isrextern void uart0_isr(void);#endif /_ISR_H/* End of isr.h */
限制150内