本帖最后由 ziqidongsheng 于 2016-9-2 11:14 编辑
这是我用寄存器写的程序,但是实现不了功能,有哪位大神会的请指教,谢谢
#include <stm8l052c6.h>
#include <stdio.h>
/*********函数声明********/
void delay(unsigned int time);
/******USART初始化*********/
void USART1_Init(void)
{
CLK_PCKENR1 =0x0f;
USART1_CR1=0x00; //设置M字长,8位数据位
USART1_CR2=0x2c; //使能发送、接收;
USART1_CR3=0x00; //1位停止位
USART1_BRR2=0x03; //设置波特率为9600
USART1_BRR1=0x68;
}
void Uart_SendData(unsigned char data)
{
while(!(USART1_SR&0X80)); //判断发送数据寄存器是否为空
USART1_DR = data; //向发送寄存器写入数据
}
/*********IO初始化*********/
void IO_Init(void)
{
PD_ODR=0x03; //初始使小灯全灭
PD_DDR=0x03; //设置PD端口为输出模式
PD_CR1=0x03; //设置PD端口为推挽输出
PD_CR2=0x00; //设置PD端口为低速输出
}
/*********主函数********/
void main(void)
{
_asm("sim"); //关总中断
USART1_Init(); //通用串行接口USART1初始化
IO_Init(); //GPIO端口初始化
CLK_CKDIVR=0x00; //系统HSI时钟1分频
_asm("rim"); //开总中断
while(1)
{
PD_ODR=0x02; //PD1(红)亮
delay(65535); //延时1s
PD_ODR=0x00; //PD1(红)灭
delay(65535); //延时1s
}
}
/******中断服务程序********/
#pragma vector = USART_R_RXNE_vector
@far @interrupt void USART1_Rx_IRQHandler(void)
{
unsigned char ch1;
if(USART1_SR =0x20) //发送是否完成
{
ch1 = USART1_DR;
Uart_SendData(ch1);
}
}
/********延时函数**********/
void delay(unsigned int time)
{
while(time--);
}
中断函数修改:
/* 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_IRQHandler(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_IRQHandler}, /* irq28 */
{0x82, NonHandledInterrupt}, /* irq29 */
};
想要实现的功能是这样的:上电后呼吸灯不断循环闪烁,当通过串口助手发送数据时进入中断,呼吸灯不再闪烁,同时串口助手也显示反馈回来的发送数据,收发完毕后又返回主函数(呼吸灯继续闪烁)。
不是的,你这样写USART1_CR2的值最终是0x20,而不是0x2c,C语言基础这是,改成:
USART1_CR2 |= 0x08; //发送使能 (USART1_CR2_TEN=0x08;)
USART1_CR2 |= 0x04; //接收使能(USART1_CR2_REN=0x04;)
USART1_CR2 |= 0x80; //发送中断使能(USART1_CR2_TIEN=0x80;)
USART1_CR2 |= 0x20; //接收中断使能(USART1_CR2_RIEN=0x20;)
一周热门 更多>