通信与信息工程学院
2014/2015 学年 第 1 学期
课程设计II 实验报告
模 块 名 称 单片机串行口通信 专 业 电子信息工程 学 生 班 级 学 生 学 号 学 生 姓 名 指 导 教 师
目 录
一.设计要求和原理说明 1.1 „设计要求 1.2 „原理说明 二.系统硬件设计 2.1 „ 2.2 „ 三.流程图设计 3.1 „ 3.2 „ 四.程序设计 4.1 „ 4.2 „ 五.设计小结
单片机串行口通信
一.设计要求和原理说明
1.1 设计要求
触摸按键控制老鼠走迷宫应用实验 基本要求:
A.MSP430F5529单片机上电后,LCD液晶显示“Slide fingers on touch pads”。 B.设计一个在LCD上显示的迷宫图。用一个符号表示老鼠。 C.触摸PAD电容按键,控制老鼠移动。带老鼠走出迷宫。 发挥部分:
A.设计PC和430之间的串行口通信程序(参考USB通信实验)和通信协议,将对应的按键信息通过USB串行口输出到PC机。 正文小四号宋体 1.2 原理说明 二.系统硬件设计 2.1 „ 2.2 „ 三.流程图设计 3.1 „ 3.2 „ 四.程序设计 4.1 主程序
#include #include "msp430.h" #include "HAL_PMM.h" #include "HAL_UCS.h" #include "HAL_Board.h" #include "HAL_Buttons.h"
#include "HAL_Cma3000.h" #include "HAL_Dogs102x6.h" #include "HAL_Menu.h" #include "HAL_Wheel.h" #include "LPM.h" #include "PMM.h" #include "lab2.h" uint16_t timeoutCounter;
void migong() { Dogs102x6_horizontalLineDraw(16, 86, 1, DOGS102x6_DRAW_NORMAL);
Dogs102x6_horizontalLineDraw(23, 30, 8, DOGS102x6_DRAW_NORMAL); Dogs102x6_horizontalLineDraw(37, 79, 8, DOGS102x6_DRAW_NORMAL); Dogs102x6_horizontalLineDraw(16, 30, 15, DOGS102x6_DRAW_NORMAL); Dogs102x6_horizontalLineDraw(58, 86, 15, DOGS102x6_DRAW_NORMAL); Dogs102x6_horizontalLineDraw(23, 44, 22, DOGS102x6_DRAW_NORMAL); Dogs102x6_horizontalLineDraw(51, 79, 22, DOGS102x6_DRAW_NORMAL); Dogs102x6_horizontalLineDraw(16, 37, 29, DOGS102x6_DRAW_NORMAL);
Dogs102x6_horizontalLineDraw(44, 51, 29, DOGS102x6_DRAW_NORMAL);
Dogs102x6_horizontalLineDraw(23, 44, 36, DOGS102x6_DRAW_NORMAL); Dogs102x6_horizontalLineDraw(51, 58, 36, DOGS102x6_DRAW_NORMAL); Dogs102x6_horizontalLineDraw(65, 79, 36, DOGS102x6_DRAW_NORMAL);
Dogs102x6_horizontalLineDraw(16, 23, 43, DOGS102x6_DRAW_NORMAL); Dogs102x6_horizontalLineDraw(37, 86, 43, DOGS102x6_DRAW_NORMAL);
Dogs102x6_horizontalLineDraw(23, 79, 50, DOGS102x6_DRAW_NORMAL);
Dogs102x6_horizontalLineDraw(16, 65, 57, DOGS102x6_DRAW_NORMAL); Dogs102x6_horizontalLineDraw(79, 86, 57, DOGS102x6_DRAW_NORMAL);
Dogs102x6_horizontalLineDraw(16, 86, 64, DOGS102x6_DRAW_NORMAL);
Dogs102x6_verticalLineDraw(1, 29, 16,DOGS102x6_DRAW_NORMAL); Dogs102x6_verticalLineDraw(36, 64, 16,DOGS102x6_DRAW_NORMAL);
Dogs102x6_verticalLineDraw(29, 36, 23,DOGS102x6_DRAW_NORMAL);
Dogs102x6_verticalLineDraw(1, 8, 30,DOGS102x6_DRAW_NORMAL);
}
Dogs102x6_verticalLineDraw(15, 36, 44,DOGS102x6_DRAW_NORMAL);
Dogs102x6_verticalLineDraw(8, 22, 51,DOGS102x6_DRAW_NORMAL);
Dogs102x6_verticalLineDraw(22, 36, 58,DOGS102x6_DRAW_NORMAL);
Dogs102x6_verticalLineDraw(36, 43, 65,DOGS102x6_DRAW_NORMAL);
Dogs102x6_verticalLineDraw(57, 64, 72,DOGS102x6_DRAW_NORMAL);
Dogs102x6_verticalLineDraw(1, 8, 79,DOGS102x6_DRAW_NORMAL);
Dogs102x6_verticalLineDraw(1, 15, 86,DOGS102x6_DRAW_NORMAL); Dogs102x6_verticalLineDraw(22, 64, 86,DOGS102x6_DRAW_NORMAL);
void main(void) {
uint8_t contrast = *((unsigned char *)contrastSetpointAddress); //读取FLASH中对比度值
uint8_t brightness = *((unsigned char *)brightnessSetpointAddress);
//读取FLASH中背光值
// Initialize accelerometer offset from flash Cma3000_setAccel_offset(*((unsigned
*)accelXcalibrationAddress), //初始化加速度偏移量 *((unsigned *)accelYcalibrationAddress),
*((unsigned *)accelZcalibrationAddress));
// Stop WDT
WDTCTL = WDTPW + WDTHOLD; //关闭看门狗
// Basic GPIO initialization
Board_init(); //初始化GPIO
// Set Vcore to accomodate for max. allowed system speed SetVCore(3); //设VCore为最大
// Use 32.768kHz XTAL as reference
LFXT_Start(XT1DRIVE_0); //利用LFXT1(32.768kHZ)作为时钟参考
// Set system clock to max (25MHz)
Init_FLL_Settle(25000, 762); //利用FLL(锁频环)将系统时钟设为最大25MHZ
char
char
char
SFRIFG1 = 0; //清中断标志 SFRIE1 |= OFIE; //使能晶振失效中断
// Globally enable interrupts
__enable_interrupt(); //
// Setup real time clock
//SetupRTC(); //
// Set up LCD
Dogs102x6_init(); // Dogs102x6_backlightInit(); //
// Contrast not programed in Flash Yet
if (contrast == 0xFF) //比度值,则将对比度值设为11(默认) // Set Default Contrast contrast = 11;
// Brightness not programed in Flash Yet
if (brightness == 0xFF) //光值,则将背光值设为11(默认) // Set Default Brightness
使能全局中断 设置实时时钟 初始化LCD 背光初始化 若当前FLASH中无对若当前FLASH中无背
brightness = 11;
Dogs102x6_setBacklight(brightness); //设置初始背光值 Dogs102x6_setContrast(contrast); //设置初始对比度值 Dogs102x6_clearScreen(); //清屏
// Set up wheel
Wheel_init(); //初始化齿轮电位计 Buttons_init(BUTTON_ALL); //初始化按键 Buttons_interruptEnable(BUTTON_ALL); //使能所有按键中断 buttonsPressed = 0; //键值清零
//显示“Slide fingers on touch pads” Dogs102x6_stringDraw(1, DOGS102x6_DRAW_NORMAL);
0,
"
Slide
Finger
on
",
Dogs102x6_stringDraw(2, 0, " Touch Pads ", DOGS102x6_DRAW_NORMAL);
// Wait for button press
while (!buttonsPressed) //等待按键被按下,或者超时退出等待 {
for (timeoutCounter timeoutCounter++) {
=
0;
timeoutCounter
0xFFFF;
if (buttonsPressed) break;
__delay_cycles(1000); }
//Timeout break
break; // }
Dogs102x6_clearScreen(); // 显示迷宫 migong(); //字符
Dogs102x6_charDrawXY(87, 16, 's',DOGS102x6_DRAW_NORMAL);
// 主循环 while (1) { lab2(); // } }
4.2 void lab2(void)
超时退出 试验二程序
{
uint8_t sliderPosition = 0;
TI_CAPT_Init_Baseline(&slider); //触摸按键初始化
sliderPosition = TI_CAPT_Slider(&slider);
sliderPosition = (sliderPosition + 10) / 20; //置
//根据按键执行操作 switch (sliderPosition) {
case 1: Dogs102x6_clearScreen(); // 显示迷宫 migong2(); //字符
Dogs102x6_charDrawXY(x--, 's',DOGS102x6_DRAW_NORMAL); break;
case 2: Dogs102x6_clearScreen(); // 显示迷宫 migong2();
得到触摸按键位y,
//字符
Dogs102x6_charDrawXY(x++, 's',DOGS102x6_DRAW_NORMAL); break;
case 3: Dogs102x6_clearScreen(); // 显示迷宫 migong2(); //字符
Dogs102x6_charDrawXY(x, 's',DOGS102x6_DRAW_NORMAL); break;
case 4: Dogs102x6_clearScreen(); // 显示迷宫 migong2(); //字符
Dogs102x6_charDrawXY(x, 's',DOGS102x6_DRAW_NORMAL); break; case (255 / 17): break; } }
五.设计小结
y,
y--, y++,
设计成绩评定
通信与信息工程学院
2014/2015 学年 第 1 学期
课程设计II 实验报告
模 块 名 称 单片机串行口通信 专 业 电子信息工程 学 生 班 级 学 生 学 号 学 生 姓 名 指 导 教 师
目 录
一.设计要求和原理说明 1.1 „设计要求 1.2 „原理说明 二.系统硬件设计 2.1 „ 2.2 „ 三.流程图设计 3.1 „ 3.2 „ 四.程序设计 4.1 „ 4.2 „ 五.设计小结
单片机串行口通信
一.设计要求和原理说明
1.1 设计要求
触摸按键控制老鼠走迷宫应用实验 基本要求:
A.MSP430F5529单片机上电后,LCD液晶显示“Slide fingers on touch pads”。 B.设计一个在LCD上显示的迷宫图。用一个符号表示老鼠。 C.触摸PAD电容按键,控制老鼠移动。带老鼠走出迷宫。 发挥部分:
A.设计PC和430之间的串行口通信程序(参考USB通信实验)和通信协议,将对应的按键信息通过USB串行口输出到PC机。 正文小四号宋体 1.2 原理说明 二.系统硬件设计 2.1 „ 2.2 „ 三.流程图设计 3.1 „ 3.2 „ 四.程序设计 4.1 主程序
#include #include "msp430.h" #include "HAL_PMM.h" #include "HAL_UCS.h" #include "HAL_Board.h" #include "HAL_Buttons.h"
#include "HAL_Cma3000.h" #include "HAL_Dogs102x6.h" #include "HAL_Menu.h" #include "HAL_Wheel.h" #include "LPM.h" #include "PMM.h" #include "lab2.h" uint16_t timeoutCounter;
void migong() { Dogs102x6_horizontalLineDraw(16, 86, 1, DOGS102x6_DRAW_NORMAL);
Dogs102x6_horizontalLineDraw(23, 30, 8, DOGS102x6_DRAW_NORMAL); Dogs102x6_horizontalLineDraw(37, 79, 8, DOGS102x6_DRAW_NORMAL); Dogs102x6_horizontalLineDraw(16, 30, 15, DOGS102x6_DRAW_NORMAL); Dogs102x6_horizontalLineDraw(58, 86, 15, DOGS102x6_DRAW_NORMAL); Dogs102x6_horizontalLineDraw(23, 44, 22, DOGS102x6_DRAW_NORMAL); Dogs102x6_horizontalLineDraw(51, 79, 22, DOGS102x6_DRAW_NORMAL); Dogs102x6_horizontalLineDraw(16, 37, 29, DOGS102x6_DRAW_NORMAL);
Dogs102x6_horizontalLineDraw(44, 51, 29, DOGS102x6_DRAW_NORMAL);
Dogs102x6_horizontalLineDraw(23, 44, 36, DOGS102x6_DRAW_NORMAL); Dogs102x6_horizontalLineDraw(51, 58, 36, DOGS102x6_DRAW_NORMAL); Dogs102x6_horizontalLineDraw(65, 79, 36, DOGS102x6_DRAW_NORMAL);
Dogs102x6_horizontalLineDraw(16, 23, 43, DOGS102x6_DRAW_NORMAL); Dogs102x6_horizontalLineDraw(37, 86, 43, DOGS102x6_DRAW_NORMAL);
Dogs102x6_horizontalLineDraw(23, 79, 50, DOGS102x6_DRAW_NORMAL);
Dogs102x6_horizontalLineDraw(16, 65, 57, DOGS102x6_DRAW_NORMAL); Dogs102x6_horizontalLineDraw(79, 86, 57, DOGS102x6_DRAW_NORMAL);
Dogs102x6_horizontalLineDraw(16, 86, 64, DOGS102x6_DRAW_NORMAL);
Dogs102x6_verticalLineDraw(1, 29, 16,DOGS102x6_DRAW_NORMAL); Dogs102x6_verticalLineDraw(36, 64, 16,DOGS102x6_DRAW_NORMAL);
Dogs102x6_verticalLineDraw(29, 36, 23,DOGS102x6_DRAW_NORMAL);
Dogs102x6_verticalLineDraw(1, 8, 30,DOGS102x6_DRAW_NORMAL);
}
Dogs102x6_verticalLineDraw(15, 36, 44,DOGS102x6_DRAW_NORMAL);
Dogs102x6_verticalLineDraw(8, 22, 51,DOGS102x6_DRAW_NORMAL);
Dogs102x6_verticalLineDraw(22, 36, 58,DOGS102x6_DRAW_NORMAL);
Dogs102x6_verticalLineDraw(36, 43, 65,DOGS102x6_DRAW_NORMAL);
Dogs102x6_verticalLineDraw(57, 64, 72,DOGS102x6_DRAW_NORMAL);
Dogs102x6_verticalLineDraw(1, 8, 79,DOGS102x6_DRAW_NORMAL);
Dogs102x6_verticalLineDraw(1, 15, 86,DOGS102x6_DRAW_NORMAL); Dogs102x6_verticalLineDraw(22, 64, 86,DOGS102x6_DRAW_NORMAL);
void main(void) {
uint8_t contrast = *((unsigned char *)contrastSetpointAddress); //读取FLASH中对比度值
uint8_t brightness = *((unsigned char *)brightnessSetpointAddress);
//读取FLASH中背光值
// Initialize accelerometer offset from flash Cma3000_setAccel_offset(*((unsigned
*)accelXcalibrationAddress), //初始化加速度偏移量 *((unsigned *)accelYcalibrationAddress),
*((unsigned *)accelZcalibrationAddress));
// Stop WDT
WDTCTL = WDTPW + WDTHOLD; //关闭看门狗
// Basic GPIO initialization
Board_init(); //初始化GPIO
// Set Vcore to accomodate for max. allowed system speed SetVCore(3); //设VCore为最大
// Use 32.768kHz XTAL as reference
LFXT_Start(XT1DRIVE_0); //利用LFXT1(32.768kHZ)作为时钟参考
// Set system clock to max (25MHz)
Init_FLL_Settle(25000, 762); //利用FLL(锁频环)将系统时钟设为最大25MHZ
char
char
char
SFRIFG1 = 0; //清中断标志 SFRIE1 |= OFIE; //使能晶振失效中断
// Globally enable interrupts
__enable_interrupt(); //
// Setup real time clock
//SetupRTC(); //
// Set up LCD
Dogs102x6_init(); // Dogs102x6_backlightInit(); //
// Contrast not programed in Flash Yet
if (contrast == 0xFF) //比度值,则将对比度值设为11(默认) // Set Default Contrast contrast = 11;
// Brightness not programed in Flash Yet
if (brightness == 0xFF) //光值,则将背光值设为11(默认) // Set Default Brightness
使能全局中断 设置实时时钟 初始化LCD 背光初始化 若当前FLASH中无对若当前FLASH中无背
brightness = 11;
Dogs102x6_setBacklight(brightness); //设置初始背光值 Dogs102x6_setContrast(contrast); //设置初始对比度值 Dogs102x6_clearScreen(); //清屏
// Set up wheel
Wheel_init(); //初始化齿轮电位计 Buttons_init(BUTTON_ALL); //初始化按键 Buttons_interruptEnable(BUTTON_ALL); //使能所有按键中断 buttonsPressed = 0; //键值清零
//显示“Slide fingers on touch pads” Dogs102x6_stringDraw(1, DOGS102x6_DRAW_NORMAL);
0,
"
Slide
Finger
on
",
Dogs102x6_stringDraw(2, 0, " Touch Pads ", DOGS102x6_DRAW_NORMAL);
// Wait for button press
while (!buttonsPressed) //等待按键被按下,或者超时退出等待 {
for (timeoutCounter timeoutCounter++) {
=
0;
timeoutCounter
0xFFFF;
if (buttonsPressed) break;
__delay_cycles(1000); }
//Timeout break
break; // }
Dogs102x6_clearScreen(); // 显示迷宫 migong(); //字符
Dogs102x6_charDrawXY(87, 16, 's',DOGS102x6_DRAW_NORMAL);
// 主循环 while (1) { lab2(); // } }
4.2 void lab2(void)
超时退出 试验二程序
{
uint8_t sliderPosition = 0;
TI_CAPT_Init_Baseline(&slider); //触摸按键初始化
sliderPosition = TI_CAPT_Slider(&slider);
sliderPosition = (sliderPosition + 10) / 20; //置
//根据按键执行操作 switch (sliderPosition) {
case 1: Dogs102x6_clearScreen(); // 显示迷宫 migong2(); //字符
Dogs102x6_charDrawXY(x--, 's',DOGS102x6_DRAW_NORMAL); break;
case 2: Dogs102x6_clearScreen(); // 显示迷宫 migong2();
得到触摸按键位y,
//字符
Dogs102x6_charDrawXY(x++, 's',DOGS102x6_DRAW_NORMAL); break;
case 3: Dogs102x6_clearScreen(); // 显示迷宫 migong2(); //字符
Dogs102x6_charDrawXY(x, 's',DOGS102x6_DRAW_NORMAL); break;
case 4: Dogs102x6_clearScreen(); // 显示迷宫 migong2(); //字符
Dogs102x6_charDrawXY(x, 's',DOGS102x6_DRAW_NORMAL); break; case (255 / 17): break; } }
五.设计小结
y,
y--, y++,
设计成绩评定