在线时间9 小时
UID2067408
ST金币0
蝴蝶豆0
注册时间2014-1-6
初级会员
- 最后登录
- 2018-10-25
|
发表于 2015-5-12 15:06:14
|
显示全部楼层
a0a.1 0b0c
MAIN.C file
*
* Copyright (c) 2002-2005 STMicroelectronics
*/
#include "stm8l15x.h"
#include "stm8l15x_tim2.h"
#include "stm8l15x_usart.h"
#define TIM2_PERIOD 999
#define TIM2_PRESCALER 2
int Nms=0,base100us=0;
int Ns=0,base1ms=0;
_Bool LEDFLAG;
void SYSCLK_Config(void)
{
CLK->SWR=0x01;
CLK->CKDIVR=0x02; //16MHz /64
}
void UART_CONFIG(void)
{
CLK->PCKENR1|=0x20;
USART_Init(USART1,9600,USART_WordLength_8b,USART_StopBits_1,USART_Parity_No,USART_Mode_Tx|USART_Mode_Rx);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART1->CR1 &= (uint8_t)(~USART_CR1_USARTD);
}
void TIM2_Config(void)
{
u16 TIM2_Period;
TIM2_Period=TIM2_PERIOD;
CLK->PCKENR1|=0x01;
TIM2->ARRH = (uint8_t)(TIM2_Period >> 8) ;
TIM2->ARRL = (uint8_t)(TIM2_Period);
/* Set the Prescaler value */
TIM2->PSCR = (uint8_t)TIM2_PRESCALER;
/* Select the Counter Mode*/
TIM2->CR1 &= (uint8_t)((uint8_t)(~TIM_CR1_CMS)) & ((uint8_t)(~TIM_CR1_DIR));
TIM2->CR1 |= (uint8_t)(TIM2_CounterMode_Up);
/* Generate an update event to reload the Prescaler value immediately */
TIM2->EGR = TIM2_EventSource_Update;
TIM2->IER |= (uint8_t)TIM2_IT_Update;//ENABLE IT
TIM2->CR1 |= 0x01;
}
main()
{
SYSCLK_Config();
TIM2_Config();
UART_CONFIG();
CFG->GCR=0x00;
GPIOB->DDR=0X02;
GPIOB->CR1=0X06;
GPIOB->CR2=0X06;// allow pb2 trige interrupter
_asm("rim");
GPIOB->ODR|=0X02;
Ns=0;
while (1)
{
if(LEDFLAG)GPIOB->ODR|=0X02;
else GPIOB->ODR&=~0X02;
if(Nms>1000)
{
Nms=0;
printf("\r \n main fun running");
}
if(Ns>5)
{
Ns=0;
printf("\r\n halt");
GPIOB->CR2|=0X04;
GPIOB->ODR&=~0X02;
_asm("halt");
}
}
}
@far @interrupt void TIM2_1ms_interrupter (void)
{
TIM2->SR1=0X00;
//LEDFLAG=~LEDFLAG;
Nms++;
base1ms++;
if(base1ms==1000)
{
LEDFLAG=~LEDFLAG;
base1ms=0;
Ns++;
}
}
@far @interrupt void PB2_interrupter(void)
{
EXTI->SR1&=~0X04;
GPIOB->CR2&=~0X04;
}
char putchar(char c)
{
USART_SendData8(USART1, (unsigned char) c);
while (!(USART1->SR & USART_FLAG_TXE));
return (c);
} |
|