历史上的今天
今天是:2024年09月02日(星期一)
2021年09月02日 | STM8S103串口通信初始化设置
2021-09-02 来源:eefocus
硬件:
STM8单片机(很裸,就一个单片机加俩电容)
ST-Link V2仿真器
PL2103 USB转TTL小板
声明部分
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,还有就是主函数里记得开中断啊,不然没有信号的!!!
史海拾趣
|
21个详细且精湛的模拟电子技术问答,内容包括: 01 电压基准及时间基准 O2 压频转换器 03 高速比较器 04 运算放大器 05 数模转换器 06 Σ ?Δ模数转换器 07 数据转换器的噪声及其它问题 08 运算放大器的噪声 09 运算放大器的建立时间 10 串行数据转 ...… 查看全部问答> |
|
知名公司招聘硬件产品工程师,要求有硬件电路设计经验,熟练使用各种电子测试设备。 如下jd,有意者可咨询alice Job Title Hardware PE (Shanghai) Roles and Responsibilities 1. This position will be to work as a member of ...… 查看全部问答> |
|
http://blog.ednchina.com/Upload/Blog/2007/3/30/826adf84-7829-4de7-8065-2149d80d0c85.jpg 请高手帮我分析下这个MP3原理图中各个组成部分的原理、处理器的功能。 谢谢!… 查看全部问答> |
|
那位高手帮个忙,char bdata LT_REG48H = 0;sbit LT_CRC_ERROR = LT_REG48H^7;sbit LT_FEC23_ERROR = LT_REG48H^6;sbit LT_FRAMER_ST_5 = LT_REG48H^5;sbit LT_FRAMER_ST_4 = LT_REG48H^4;sbit LT_FRAMER_ST_3 = LT_REG48H^3;sbit LT_FRAMER_ST_2 = ...… 查看全部问答> |
|
主函数中是一个蓝色LED灯常亮,当定时器溢出后,跳到中断函数另一个红色LED灯亮。想实现两个LED灯交替闪亮。结果开始蓝灯亮,接着红灯亮,但以后就红灯一直亮了。。。 程序中有个SysTick做延时用,可以忽略 程序如下: 主函数 #include \"stm ...… 查看全部问答> |




