高级会员
- 积分
- 839
- 金钱
- 839
- 注册时间
- 2016-8-23
- 在线时间
- 116 小时
|
发表于 2016-9-1 09:47:16
|
显示全部楼层
本帖最后由 紫气东升 于 2016-9-7 15:41 编辑
#include <stm8l052c6.h>
#include <stdio.h>
void USART1_Init(unsigned int baudrate)
{
unsigned int baud; //
//CLK_PCKENR1_bit.PCKEN15 = 1;
CLK_PCKENR1 =0x20;
baud = 16000000 / baudrate;
USART1_BRR2 =0x03;// ((unsigned char)((baud & 0xf000) >> 8 )) | ((unsigned char)(baud & 0x000f));
USART1_BRR1 =0x68;// ((unsigned char)((baud & 0x0ff0) >> 4));
/*USART1_CR1_bit.USARTD = 0;
USART1_CR2_bit.RIEN = 1;
USART1_CR2_bit.TEN = 1;
USART1_CR2_bit.REN = 1;*/
USART1_CR1=0x00;
USART1_CR2=0x2c;
USART1_CR3=0x00;
}
void USART1_SendData(unsigned char data)
{
while(!(USART1_SR&0X80)); //判断发送数据寄存器是否为空
USART1_DR = data; //向发送寄存器写入数据
}
/* 主函数 */
main()
{
_asm ("sim");
CLK_CKDIVR = 0x00;
USART1_Init(9600);
_asm ("rim");
while(1)
{
}
}
/* 串口接收数据中断服务函数 */
#pragma vector = USART1_R_RXNE_vector
@far @interrupt void USART1_RX_RXNE(void)
{
unsigned char ch1;
if(USART1_SR =0x20 /*USART1_SR_bit.RXNE == 1*/)
{
ch1 = USART1_DR; //读出串口接收到的数据 清除中断标志
USART1_SendData(ch1); //把接收到的数据再通过串口发送出去
}
}
中断函数修改:
/* BASIC INTERRUPT VECTOR TABLE FOR STM8 devices
* Copyright (c) 2007 STMicroelectronics
*/
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 */
@far @interrupt void USART1_RX_RXNE(void);
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, NonHandledInterrupt}, /* 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, USART1_RX_RXNE}, /* irq28 */
{0x82, NonHandledInterrupt}, /* irq29 */
};
workspace:
source files:stm8l052c6.h main.c stm8_interrupt_vector.c
external dependencies:mods0.h stdio.h
这是我修改过的程序,但是还是没有成功
|
|