本帖最后由 wuwenf 于 2014-6-8 12:24 编辑
#include "STM8S105C_S.h" /* Registers and memory mapping file. */
unsigned char RxBuffer = 2;
unsigned char ch;
unsigned char ch1;
void UART2_Init(void);
void UART2_Send(unsigned char TxData);
void main(void)
{
unsigned int i;
unsigned char counter;
_asm("sim"); //关全局中断
/*High speed internal clock prescaler: 1*/
CLK_CKDIVR = 0x00;
UART2_Init();
_asm("rim");//开全局中断
while(1)
{
for (i=0;i<1000;i++);
UART2_Send(RxBuffer);
}
}
void UART2_Init(void)
{
UART2_CR2 = 0; // 禁止UART发送和接收
UART2_CR1 = 0; //b5 = 0,允许UART,禁止奇偶校验
//一个起始位,8个数据位
UART2_CR3 = 0; // 1个停止位
UART2_BRR2 = 0;
UART2_BRR1 = 0x0d; //实际的波特率分频系数为00D0 对应的波特率为2000000/208=960
UART2_CR2 = 0x2C; //b3 = 1,允许发送 b2 = 1,允许接收
}
////
@far @interrupt void UART2_Recv_Inter(void)
{
ch = UART2_DR; // 读入接收到的字符
UART2_SR &= ~0x20;
return;
}
void UART2_Send(unsigned char TxData)
{
while((UART2_SR & 0x80) == 0x00); // 若发送寄存器不空,则等待
UART2_DR = TxData; // 将要发送的字符送到数据寄存器
}
////////////中断
extern @far @interrupt void UART2_Recv_Inter(void);
struct interrupt_vector const _vectab[] = {
{0x82, (interrupt_handler_t)_stext}, /*RESET reset */
{0x82, NonHandledInterrupt}, /* TRAP trap */
{0x82, NonHandledInterrupt}, /* TLI irq0 */
{0x82, NonHandledInterrupt}, /* AWU irq1 */
{0x82, NonHandledInterrupt}, /* CLK irq2 */
{0x82, NonHandledInterrupt}, /* EXTI0_A irq3 */
{0x82, NonHandledInterrupt}, /* EXTI1_B irq4 */
{0x82, NonHandledInterrupt}, /* EXTI2_C irq5 */
{0x82, NonHandledInterrupt}, /* EXTI3_D irq6 */
{0x82, NonHandledInterrupt}, /* EXTI4_E irq7 */
{0x82, NonHandledInterrupt}, /* CAN_RX irq8 */
{0x82, NonHandledInterrupt}, /* CAN_TX irq9 */
{0x82, NonHandledInterrupt}, /* SPI_RXTX irq10 */
{0x82, NonHandledInterrupt}, /* TIM1 irq11 TIM1_irq11*/
{0x82, NonHandledInterrupt}, /* TIM1_CCP irq12 */
{0x82, NonHandledInterrupt}, /* TIM2 irq13 */
{0x82, NonHandledInterrupt}, /* TIM2_CCP irq14 */
{0x82, NonHandledInterrupt}, /* TIM3 irq15 */
{0x82, NonHandledInterrupt}, /* TIM3_CCP irq16 */
{0x82, NonHandledInterrupt}, /* UART1_TX irq17 */
{0x82, NonHandledInterrupt}, /* UART1_RX irq18 */
{0x82, NonHandledInterrupt}, /* I2C_RXTX irq19 */
{0x82, NonHandledInterrupt}, /* UART2_TX irq20 */
{0x82, UART2_Recv_Inter}, /* UART2_RX irq21 */
{0x82, NonHandledInterrupt}, /* ADC1 irq22 */
{0x82, NonHandledInterrupt}, /* TIM4 irq23 */
{0x82, NonHandledInterrupt}, /* FLASH irq24 */
{0x82, NonHandledInterrupt}, /* irq25 */
{0x82, NonHandledInterrupt}, /* irq26 */
{0x82, NonHandledInterrupt}, /* irq27 */
{0x82, NonHandledInterrupt}, /* irq28 */
{0x82, NonHandledInterrupt}, /* irq29 */
}; |
|