【STM8】多个单片机级联 UART协议 主从自主判断

项目分析

#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)只需修改主模条程序,来实现升级

  1. 灵活的模块拼接(不用刻意培训安装人员)
    主从自主判断,常用的方法就是上电检测,确认主从关系
  2. 容错率要高
    从设备一段时间接收不到数据,重新分配主从。这就用到看门狗了。
  3. 预先考虑项目升级
    升级的时候要尽量少的改动,只更改主单片机的程序,从模块不用修改,就能实现升级。这样要显示的内容只能由主设备发给从设备

代码实现

stm8控制MBI5043的文章基础上进一步作业,只需要实现多个stm8通过UART实现通信的功能就可以了

  1. 外设的使用
    记得初始化三部曲----时钟,引脚,外设,用到了新的外设,对应引脚和时钟也记得初始化
  • 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值