项目分析
#define n_MBI5043_1_stm8 8 //一根模条有一个stm8带载8个LED驱动芯片
#define LEDs 40 //1个stm8驱动了40个灯
#define Colors 3 //每个灯有3个led灯组成
#define STMs 3 //3个stm8通过uart级联实现
本程序实现(1)uart Tx线中断恢复后能正常显示(2)只需修改主模条程序,来实现升级
- 灵活的模块拼接(不用刻意培训安装人员):
主从自主判断,常用的方法就是上电检测,确认主从关系 - 容错率要高:
从设备一段时间接收不到数据,重新分配主从。这就用到看门狗了。 - 预先考虑项目升级:
升级的时候要尽量少的改动,只更改主单片机的程序,从模块不用修改,就能实现升级。这样要显示的内容只能由主设备发给从设备
代码实现
stm8控制MBI5043的文章基础上进一步作业,只需要实现多个stm8通过UART实现通信的功能就可以了
- 外设的使用
记得初始化三部曲----时钟,引脚,外设,用到了新的外设,对应引脚和时钟也记得初始化
- UART外设的初始化和发送模块先写好,以后就可以直接调用了
类似之前使用SPI外设的方法,查阅datasheet就可以了。
#include "STM8S103F.h"
void uart1_init(void)
{
UART1_CR1=0x00;
UART1_CR3=0x00;
UART1_CR2=0x00;
UART1_BRR2=0x0b;
UART1_BRR1=0x08; //波特率9600
UART1_SR &= ~0x20;
UART1_CR2=0x2c; //允许发送和接收
}
void uart1_send(unsigned char c)
{
while(!(UART1_SR&0x80));
UART1_DR = c;
}
- 用到了看门狗外设
#include "STM8S103F.h"
void IWDG_Init(void) //配置并启动看门狗
{
IWDG_KR = 0xcc;//启动独立看门狗
IWDG_KR = 0x55;//写入解锁
IWDG_PR = 0x06;//256分频
IWDG_RLR = 0xff;//设置重载寄存器
IWDG_KR = 0xaa;//锁定并刷新
}
void IWDG_Feed(void)//喂狗
{
IWDG_KR = 0xaa;
}
- 用到了uart接收中断
--------------------------------开总中断--------------------------
_asm("rim");
---------------------根据datasheet编写stm8_interrupt_vector-------
/* BASIC INTERRUPT VECTOR TABLE FOR STM8 devices
* Copyright (c) 2007 STMicroelectronics
*/
#include "func.h"
#include "public.h"
#include "STM8S103F.h"
#include "string.h"
typedef void @far (*interrupt_handler_t)(void);
struct interrupt_vector {
unsigned char interrupt_instruction;
interrupt_handler_t interrupt_handler;
};
@far @interrupt void NonHandledInterrupt (void)
{
/* in order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction
*/
return;
}
extern void _stext(); /* startup routine */
struct interrupt_vector const _vectab[] = {
{
0x82, (interrupt_handler_t)_stext}, /* reset */
{
0x82, NonHandledInterrupt}, /* trap */
{
0x82, NonHandledInterrupt}, /* irq0 */
{
0x82, NonHandledInterrupt}, /* irq1 */
{
0x82, NonHandledInterrupt}, /* irq2 */
{
0x82, NonHandledInterrupt}, /* irq3 */
{
0x82, NonHandledInterrupt}, /* irq4 */
{
0x82, NonHandledInterrupt}, /* irq5 */
{
0x82, NonHandledInterrupt}, /* irq6 */
{
0x82, NonHandledInterrupt}, /* irq7 */
{
0x82, NonHandledInterrupt}, /* irq8 */
{
0x82, NonHandledInterrupt}, /* irq9 */
{
0x82, NonHandledInterrupt}, /* irq10 */
{
0x82, NonHandledInterrupt}, /* irq11 */
{
0x82, NonHandledInterrupt}, /* irq12 */
{
0x82, NonHandledInterrupt}, /* irq13 */
{
0x82, NonHandledInterrupt}, /* irq14 */
{
0x82, NonHandledInterrupt}, /* irq15 */
{
0x82, NonHandledInterrupt}, /* irq16 */
{
0x82, NonHandledInterrupt}, /* irq17 */
{
0x82, UART1_Recv_IRQHandler}, /* irq18 */
{
0x82, NonHandledInterrupt}, /* irq19 */
{
0x82, NonHandledInterrupt}, /* irq20 */
{
0x82, NonHandledInterrupt}, /* irq21 */
{
0x82, NonHandledInterrupt}, /* irq22 */
{
0x82, NonHandledInterrupt}, /* irq23 */
{
0x82, NonHandledInterrupt}, /* irq24 */
{
0x82, NonHandle