OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 9457|回复: 5

求个STM8的模拟串口的代码。

[复制链接]

1

主题

1

帖子

0

精华

新手上路

积分
32
金钱
32
注册时间
2014-10-31
在线时间
1 小时
发表于 2014-10-31 16:31:48 | 显示全部楼层 |阅读模式
5金钱
最近学STM8,模拟串口搞不出来,各位大神帮帮忙,给段STM8的模拟串口的代码 要能用的。

最佳答案

查看完整内容[请看2#楼]

/* Includes ------------------------------------------------------------------*/ #include "stm8s.h" #include "stm8s_it.h" //#include "iostm8s103k3.h" #include "main.h" /////////////////////////////////////////////////// #define TIM1_timer 1667  //接收定时器 #define TIM2_timer 1667 //发送定时器 u8 vm_UART_RX_P;//接收缓存指针 #define vm_UART_RX_BUF_L 32//接收缓存长度 u8 vm_U ...
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

14

主题

47

帖子

0

精华

初级会员

Rank: 2

积分
143
金钱
143
注册时间
2012-7-12
在线时间
13 小时
发表于 2014-10-31 16:31:49 | 显示全部楼层


/* Includes ------------------------------------------------------------------*/
#include "stm8s.h"

#include "stm8s_it.h"
//#include "iostm8s103k3.h"
#include "main.h"

///////////////////////////////////////////////////
#define TIM1_timer 1667  //接收定时器
#define TIM2_timer 1667 //发送定时器

u8 vm_UART_RX_P;//接收缓存指针
#define vm_UART_RX_BUF_L 32//接收缓存长度
u8 vm_UART_RX_BUF[vm_UART_RX_BUF_L];//接收缓存

u8 vm_UART_RX_byte;//扩展串口 字节缓冲区
u8 vm_UART_RX_bit;//扩展串口 计算位数

u8 vm_UART_TX_byte;//扩展串口 字节缓冲区
u8 vm_UART_TX_bit;//扩展串口 计算位数

u8 vm_uart_tx_flag;//正在发送标志
u8 vm_uart_rx_flag;//正在接收标志
#define TIM2_START         TIM2->CNTRL = 0;TIM2->CNTRH = 0;TIM2->CR1 |= TIM2_CR1_CEN;  //计数器置零,启动定时器
#define TIM2_STOP             (TIM2->CR1 &=  (~TIM2_CR1_CEN)) //停止定时器

#define TIM1_START         TIM1->CNTRL = 0;TIM1->CNTRH = 0;TIM1->CR1 |= TIM1_CR1_CEN;  //计数器置零,启动定时器
#define TIM1_STOP             (TIM1->CR1 &=  (~TIM1_CR1_CEN)) //停止定时器

#define VM_UART_TXD_PORT_WriteHigh     GPIOD->ODR |= GPIO_PIN_4
#define VM_UART_TXD_PORT_WriteLow     GPIOD->ODR &= (~GPIO_PIN_4)
#define VM_UART_TXD_PORT_OUT       GPIOD->DDR |=   GPIO_PIN_4;GPIOD->CR1 |=  (GPIO_PIN_4);GPIOD->CR2 &=  (~GPIO_PIN_4) //设定为输出
#define VM_UART_TXD_PORT_IN       GPIOD->DDR &= ~(GPIO_PIN_4);GPIOD->CR1 |=  (GPIO_PIN_4);GPIOD->CR2 &=  (~GPIO_PIN_4)  //设定为输出

#define VM_UART_RXD_PORT_IN        GPIOD->DDR &= ~(GPIO_PIN_3);GPIOD->CR1 |=  (GPIO_PIN_3);GPIOD->CR2 &=  (~GPIO_PIN_3) //只上拉输入 不中断
//设置为输入
#define VM_UART_RXD_PORT_INT_IN    GPIOD->DDR &= ~(GPIO_PIN_3);GPIOD->CR1 |=  (GPIO_PIN_3);GPIOD->CR2 |=  (GPIO_PIN_3) //只上拉输入 不中断
//设置为可中断输入
////////////////////////////////////////////////////////////////////////////////////////////////
INTERRUPT_HANDLER_TRAP(TRAP_IRQHandler)
{
}
INTERRUPT_HANDLER(TLI_IRQHandler, 0)
{
}
INTERRUPT_HANDLER(AWU_IRQHandler, 1)
{
}
INTERRUPT_HANDLER(CLK_IRQHandler, 2)
{
}
INTERRUPT_HANDLER(EXTI_PORTA_IRQHandler, 3)
{
}
INTERRUPT_HANDLER(EXTI_PORTB_IRQHandler, 4)
{
}
INTERRUPT_HANDLER(EXTI_PORTC_IRQHandler, 5)
{
}
INTERRUPT_HANDLER(EXTI_PORTD_IRQHandler, 6)
{
    //外中断一次收一个字节,只识别起始位
    if((GPIOD->IDR & (uint8_t)GPIO_PIN_3) == 0)
    {
        TIM1_START;//启动定时器
        vm_UART_RX_byte = 0;
        vm_UART_RX_bit = 0;
        VM_UART_RXD_PORT_IN; //只上拉输入 不中断
        vm_uart_rx_flag = 1;
    }
}
INTERRUPT_HANDLER(EXTI_PORTE_IRQHandler, 7)
{}
#ifdef STM8S903
INTERRUPT_HANDLER(EXTI_PORTF_IRQHandler, 8)
{}
#endif /*STM8S903*/

#ifdef STM8S208
INTERRUPT_HANDLER(CAN_RX_IRQHandler, 8)
{}
INTERRUPT_HANDLER(CAN_TX_IRQHandler, 9)
{}
#endif /*STM8S208 || STM8AF52Ax */
INTERRUPT_HANDLER(SPI_IRQHandler, 10)
{}
INTERRUPT_HANDLER(TIM1_UPD_OVF_TRG_BRK_IRQHandler, 11)//接收
{
    TIM1->SR1 = (uint8_t)(~TIM1_IT_UPDATE);
    if((GPIOD->IDR & GPIO_PIN_3) == 0)
    {
        vm_UART_RX_byte /= 2;
    }
    else
    {
        vm_UART_RX_byte /= 2;
        vm_UART_RX_byte |= 0X80;
    }
    vm_UART_RX_bit++;
    if(vm_UART_RX_bit >= 8)
    {
        GPIO_WriteReverse(GPIOB, GPIO_PIN_4);
        TIM1_STOP;//停止定时器
        VM_UART_RXD_PORT_INT_IN;
        vm_uart_rx_flag = 0;
        vm_UART_RX_bit = 0;//
        vm_UART_RX_BUF[vm_UART_RX_P] = vm_UART_RX_byte; //一个字节时接收完毕
        vm_UART_RX_P++;
        if(vm_UART_RX_P >= vm_UART_RX_BUF_L)vm_UART_RX_P = 0; //接收指针
    }
}
INTERRUPT_HANDLER(TIM1_CAP_COM_IRQHandler, 12)
{}
#ifdef STM8S903
INTERRUPT_HANDLER(TIM5_UPD_OVF_BRK_TRG_IRQHandler, 13)
{}
INTERRUPT_HANDLER(TIM5_CAP_COM_IRQHandler, 14)
{}
#else /*STM8S208, STM8S207, STM8S105 or STM8S103 or STM8AF62Ax or STM8AF52Ax or STM8AF626x */
//////////////////////////////////////////////////////////////////////////////////////////////////
INTERRUPT_HANDLER(TIM2_UPD_OVF_BRK_IRQHandler, 13)
{
    TIM2->SR1 = (uint8_t)(~TIM2_IT_UPDATE);

    if(vm_UART_TX_bit < 8)
    {
        if((vm_UART_TX_byte & 0x1) == 0x1)
        {
            VM_UART_TXD_PORT_WriteHigh;
        }
        else
        {
            VM_UART_TXD_PORT_WriteLow;
        }
        vm_UART_TX_byte /= 2;
    }
    else
    {
        VM_UART_TXD_PORT_WriteHigh;
        if(vm_UART_TX_bit > 8)
        {
            vm_uart_tx_flag = 0; //设置为发送完毕
            VM_UART_TXD_PORT_IN;
            TIM2_STOP;//
        }
    }
    vm_UART_TX_bit++;
}

void vm_UARTsend_byte(u8 byte)
{
    while(vm_uart_tx_flag == 1); //检查是否发送完毕

    vm_uart_tx_flag = 1;
    VM_UART_TXD_PORT_OUT;
    VM_UART_TXD_PORT_WriteLow;
    vm_UART_TX_bit = 0;
    vm_UART_TX_byte = byte;
    TIM2_START; //计数器置零,启动定时器
}
void vm_UART_SendString(u8 *Data, u16 len)
{
    u16 i = 0;
    for(; i < len; i++)
        vm_UARTsend_byte(Data);

}
void vm_UART_SendStr(u8 *str)
{
    u16 i = 0;
    while((*(str + i)) != 0)
    {
        vm_UARTsend_byte(*(str + i));
        i++;
    }
}
//////////////////////////////////////////////////////////////////////////////////
INTERRUPT_HANDLER(TIM2_CAP_COM_IRQHandler, 14)
{
}
#endif /*STM8S903*/

#if defined (STM8S208) || defined(STM8S207) || defined(STM8S105)
INTERRUPT_HANDLER(TIM3_UPD_OVF_BRK_IRQHandler, 15)
{
}

INTERRUPT_HANDLER(TIM3_CAP_COM_IRQHandler, 16)
{
}
#endif /*STM8S208, STM8S207 or STM8S105 or STM8AF62Ax or STM8AF52Ax or STM8AF626x */

#if defined (STM8S208) || defined(STM8S207) || defined(STM8S103) || defined (STM8AF62Ax) ||\
defined (STM8AF52Ax) || defined (STM8S903)
INTERRUPT_HANDLER(UART1_TX_IRQHandler, 17)
{
}

INTERRUPT_HANDLER(UART1_RX_IRQHandler, 18)
{

}
#endif /*STM8S105*/

INTERRUPT_HANDLER(I2C_IRQHandler, 19)
{
}

#if defined (STM8S105) || defined (STM8AF626x)
INTERRUPT_HANDLER(UART2_TX_IRQHandler, 20)
{
}

INTERRUPT_HANDLER(UART2_RX_IRQHandler, 21)
{
}
#endif /* STM8S105*/

#if defined(STM8S207) || defined(STM8S208) || defined (STM8AF52Ax) || defined (STM8AF62Ax)
INTERRUPT_HANDLER(UART3_TX_IRQHandler, 20)
{
}

INTERRUPT_HANDLER(UART3_RX_IRQHandler, 21)
{
}
#endif /*STM8S208 or STM8S207 or STM8AF52Ax or STM8AF62Ax */

#if defined(STM8S207) || defined(STM8S208) || defined (STM8AF52Ax) || defined (STM8AF62Ax)

INTERRUPT_HANDLER(ADC2_IRQHandler, 22) {}
#else /*STM8S105, STM8S103 or STM8S903 or STM8AF626x */
INTERRUPT_HANDLER(ADC1_IRQHandler, 22)
{}
#endif /*STM8S208 or STM8S207 or STM8AF52Ax or STM8AF62Ax */

#ifdef STM8S903
INTERRUPT_HANDLER(TIM6_UPD_OVF_TRG_IRQHandler, 23)
{}
#else /*STM8S208, STM8S207, STM8S105 or STM8S103 or STM8AF62Ax or STM8AF52Ax or STM8AF626x */
INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23)
{
    TIM4->SR1 = (uint8_t)(~TIM4_IT_UPDATE);
}
#endif /*STM8S903*/
INTERRUPT_HANDLER(EEPROM_EEC_IRQHandler, 24)
{
}

void GPIO_init(void)
{
    GPIOB->CR1 |= (uint8_t)(GPIO_PIN_4);
    GPIOB->CR2 |= (uint8_t)(GPIO_PIN_4);
    GPIOB->DDR |= (uint8_t)GPIO_PIN_4;

    GPIOB->CR1 |= (uint8_t)(GPIO_PIN_5);
    GPIOB->CR2 |= (uint8_t)(GPIO_PIN_5);
    GPIOB->DDR |= (uint8_t)GPIO_PIN_5;

    EXTI->CR1 &= (uint8_t)(~EXTI_CR1_PDIS);
    EXTI->CR1 |= (uint8_t)(0x80);//下降沿触发

    VM_UART_TXD_PORT_IN;
    VM_UART_RXD_PORT_INT_IN;

    EXTI->CR1 &= (uint8_t)(~EXTI_CR1_PAIS);
    EXTI->CR1 |= (uint8_t)(EXTI_SENSITIVITY_RISE_FALL);//上升沿和下降沿触发
    GPIOA->CR1 |= (uint8_t)(GPIO_PIN_3);
    GPIOA->CR2 |= (uint8_t)(GPIO_PIN_3);
    GPIOA->DDR &= ~(uint8_t)GPIO_PIN_3; //输入 //外部中断
}

void TIM1_Configuration(u16 time, u8 en)
{
    TIM1_TimeBaseInit(0x0000, TIM1_COUNTERMODE_UP, 0x0000, 0x00);

    /* Set the Autoreload value */
    TIM1->ARRH = (uint8_t)(time >> 8);
    TIM1->ARRL = (uint8_t)(time);

    /* Set the Prescaler value */
    TIM1->SCRH = 0;
    TIM1->SCRL = 0;

    /* Select the Counter Mode */
    TIM1->CR1 = 0;
    TIM1->CR1 = TIM1_CR1_ARPE;;//TIM2_ARRPreloadConfig(ENABLE);
    // TIM1->CR1 |= TIM1_CR1_CEN;


    /* Set the Repetition Counter value */
    // TIM1->RCR = TIM1_RepetitionCounter;

    //////////////////////////

    TIM1->IER = 1;//TIM2_ITConfig(TIM2_IT_UPDATE, ENABLE);

    if(en != 0)
    {
        TIM1->CR1 |= 1;//TIM2_Cmd(ENABLE);
    }
}
void TIM2_Configuration(u16 time, u8 en)
{
    TIM2->CR1 = (uint8_t)TIM2_CR1_ARPE;//TIM2_ARRPreloadConfig(ENABLE);

    TIM2->SCR = 0;//TIM2_TimeBaseInit(TIM2_PRESCALER_16, time);

    TIM2->ARRH = (uint8_t)(time >> 8);
    TIM2->ARRL = (uint8_t)(time & 0xff); //TIM2_SetAutoreload(time);

    TIM2->IER = 1;//TIM2_ITConfig(TIM2_IT_UPDATE, ENABLE);

    if(en != 0)
    {
        TIM2->CR1 |= 1;//TIM2_Cmd(ENABLE);
    }


void main(void)

    u8 vm_UART_RX_TO_UART;
 
 
    GPIO_init(); 

    TIM2_Configuration(TIM2_timer, 0);
    TIM1_Configuration(TIM1_timer, 0);

    enableInterrupts();                      //开全局中断 

    for(;;)
    {
   
 
        while(vm_UART_RX_TO_UART != vm_UART_RX_P)
        {

            vm_UARTsend_byte(vm_UART_RX_BUF[vm_UART_RX_TO_UART]);
            vm_UART_RX_TO_UART++;
            if(vm_UART_RX_TO_UART >= vm_UART_RX_BUF_L) vm_UART_RX_TO_UART = 0;

        }


    }
}

#ifdef USE_FULL_ASSERT

/**
* @brief  Reports the name of the source file and the source line number
*   where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval : None
*/
void assert_failed(u8 *file, u32 line)
{
    /* User can add his own implementation to report the file name and line number,
    ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

    /* Infinite loop */
    while (1)
    {
    }
}
#endif

/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/

回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165309
金钱
165309
注册时间
2010-12-1
在线时间
2108 小时
发表于 2014-10-31 23:25:34 | 显示全部楼层
帮顶。。。。
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复

使用道具 举报

0

主题

15

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
359
金钱
359
注册时间
2016-10-31
在线时间
50 小时
发表于 2017-3-21 16:22:03 | 显示全部楼层
好东西
回复

使用道具 举报

4

主题

24

帖子

0

精华

初级会员

Rank: 2

积分
174
金钱
174
注册时间
2014-12-10
在线时间
42 小时
发表于 2017-9-14 20:19:10 | 显示全部楼层
好东西
回复

使用道具 举报

18

主题

155

帖子

0

精华

论坛元老

Rank: 8Rank: 8

积分
3882
金钱
3882
注册时间
2016-7-8
在线时间
817 小时
发表于 2017-11-30 17:46:25 | 显示全部楼层
请问这个代码可以用吗?我下载进去不能 收发,不知道是哪里的原因
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2024-11-22 19:22

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表