初级会员
- 积分
- 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****/
|
|