硬件:
STM8单片机(很裸,就一个单片机加俩电容)
ST-Link V2仿真器
PL2103 USB转TTL小板
感谢极品茶的STM8S 串口初始化设置
声明部分
uart.h
- #ifndef __UART_H__
- #define __UART_H__
- //#include “stm8s.h”
- #include “define.h”
- //Fcpu=8M时波特率设置
- #define UART_RATE_1200 (uint16)6666 //0x1a0a
- #define UART_RATE_2400 (uint16)3333 //0xd05
- #define UART_RATE_4800 (uint16)1667 //0x683
- #define UART_TARE_9600 (uint16)833 //0x341
- #define UART_RATE_19200 (uint16)416 //0x1a0
- //UART1_SR
- #define TXE (uint8)(1<<7) //发送数据寄存器空
- #define TC (uint8)(1<<6) //发送完成
- #define RXNE (uint8)(1<<5) //读数据寄存器非空
- //UART1_CR2
- #define TIEN (1<<7) //发送中断使能
- #define TCIEN (1<<6) //发送完成中断使能
- #define RIEN (1<<5) //接收中断使能
- #define TEN (1<<3) //发送使能
- #define REN (1<<2) //接收使能
- //UART1_CR3
- #define UARTSTOP_1BIT (0<<4)
- #define UARTSTOP_2BIT (2<<4)
- #define UARTSTOP_15BIT (3<<5)
- extern void uart1_init(uint16 tcon);
- extern void uart1_send_byte(uint8 byte);
- extern uint8 uart1_rece_byte(uint8 *a);
- extern void uart1_send_string(uint8 *a, uint8 datlong);
- #endif
函数部分
uart.c
- /*
- * – usart.c
- */
- #include “uart.h”
- //****************************
- //函数名称:uart1_init
- //函数功能:串口寄存器初始化
- //入口参数:波特率值
- //出口参数:无
- //返 回 值:无
- //****************************
- void uart1_init(uint16 tcon)
- {
- uint8 Temp1 = 0;
- uint8 Temp2 = 0;
- //禁止UART发送和接收
- UART1_CR2 = 0;
- //M=0, 8个数据位; b2=0, 禁止校验; b5=0, UART使能
- UART1_CR1 = 0;
- UART1_CR3 = 0; //b5 b4=00, 一个停止位
- //设置波特率时注意:
- //1,必须先写BRR2
- //2,BRR1存放的是分频系数的第11位到第4位
- //3,BRR2存放的是分频系数的第15位到第12位,和第3位到第0位
- //对于波特率位9600时,分频系数=8000000/9600=833–>0x341
- Temp1 = (uint8)((tcon>>4)&0x00ff);
- Temp2 = (uint8)((tcon&0x000f)|((tcon>>8)&0x00f0));
- UART1_BRR2 = Temp2;
- UART1_BRR1 = Temp1;
- //允许发送,允许接收,接收中断使能
- UART1_CR2 |= (REN | TEN | RIEN);
- }
- //****************************
- //函数名称:uart1_send_byte
- //函数功能:串口发送一字节数据
- //入口参数:要发送的数据
- //出口参数:无
- //返 回 值:无
- //****************************
- void uart1_send_byte(uint8 byte)
- {
- while(!(UART1_SR&TXE))
- ; //发送数据寄存器为非空,等待
- UART1_DR = byte;
- }
- //*****************************
- //函数名称:uart1_rece_byte
- //函数功能:串口接收一字节数据
- //入口参数:无
- //出口参数:接收到的数据
- //返 回 值:返回是否接收到数据,接收到数据返回1,未接收到返回0
- //*****************************
- uint8 uart1_rece_byte(uint8 *a)
- {
- if((UART1_SR&RXNE) != 0) //读数据寄存器为非空,说明有数据进来
- {
- UART1_SR &= ~RXNE;
- *a = UART1_DR;
- return 1;
- }
- return 0;
- }
- //****************************
- //函数名称:uart_send_string
- //函数功能:串口发送一串数据
- //入口参数:要发送的数据
- //出口参数:无
- //返 回 值:无
- //****************************
- void uart1_send_string(uint8 *a, uint8 datlong)
- {
- uint8 i=0;
- for(i=0; i<datlong; i++)
- {
- while(!(UART1_SR&TXE))
- ;//发送数据寄存器为非空,等待
- UART1_DR = a[i];
- }
- }
主函数调用
main.c
- main()
- {
- //PD_DDR |= (1<<2);
- //PD_CR1 |= (1<<2);
- //PD_CR2 &= ~(1<<2);//PD2设置为推挽输出
- PD_DDR |= (1<<5);
- PD_CR1 |= (1<<5);
- PD_CR2 &= (1<<5); //PD5设置为推挽输出
- PD_DDR &= (1<<6); //PD6设置为悬浮输入
- //PD_ODR |= (1<<2);
- //clk_init();
- _asm(“rim”); //开中断
- //timer1_pwm_init();
- uart1_init(UART_TARE_9600);
- //timer2_pwm_init();
- while (1);
- }
中断函数部分
stm8_interrupt_vector.c
- /* BASIC INTERRUPT VECTOR TABLE FOR STM8 devices
- * Copyright (c) 2007 STMicroelectronics
- */
- #include “uart.h”
- typedef void @far (*interrupt_handler_t)(void);
- struct interrupt_vector {
- unsigned char interrupt_instruction;
- interrupt_handler_t interrupt_handler;
- };
- @far @interrupt void UART1_RX_IRQHandler (void)
- {
- /* in order to detect unexpected events during development,
- it is recommended to set a breakpoint on the following instruction
- */
- uint8 *a;
- uart1_rece_byte(a);
- *a += 1;
- uart1_send_byte(*a);
- return;
- }
- @far @interrupt void UART1_TX_IRQHandler (void)
- {
- return;
- }
- @far @interrupt void NonHandledInterrupt (void)
- {
- 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, UART1_TX_IRQHandler}, /* irq17 */
- {0x82, UART1_RX_IRQHandler}, /* irq18 */
- {0x82, NonHandledInterrupt}, /* irq19 */
- {0x82, NonHandledInterrupt}, /* irq20 */
- {0x82, NonHandledInterrupt}, /* irq21 */
- {0x82, NonHandledInterrupt}, /* irq22 */
- {0x82, NonHandledInterrupt}, /* irq23 */
- {0x82, NonHandledInterrupt}, /* irq24 */
- {0x82, NonHandledInterrupt}, /* irq25 */
- {0x82, NonHandledInterrupt}, /* irq26 */
- {0x82, NonHandledInterrupt}, /* irq27 */
- {0x82, NonHandledInterrupt}, /* irq28 */
- {0x82, NonHandledInterrupt}, /* irq29 */
- };
我做的调用比较简单,接收数据,然后+1发送回去。有两个问题,一个是波特率计算不对,8M内置主频,好像被分频1/4,还有就是主函数里记得开中断啊,不然没有信号的!!!