- // _ooOoo_
- // o8888888o
- // 88" . "88
- // (| -_- |)
- // O\ = /O
- // ____/`---'\____
- // .' \\| |// `.
- // / \\||| : |||// \
- // / _||||| -:- |||||- \
- // | | \\\ - /// | |
- // | \_| ''\---/'' | |
- // \ .-\__ `-` ___/-. /
- // ___`. .' /--.--\ `. . __
- // ."" '< `.___\_<|>_/___.' >'"".
- // | | : `- \`.;`\ _ /`;.`/ - ` : | |
- // \ \ `-. \_ __\ /__ _/ .-` / /
- // ======`-.____`-.___\_____/___.-`____.-'======
- // `=---='
- //
- //
- // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- // 佛祖保佑 永不死机
- // 心外无法 法外无心
* @file Project/main.c
* @author MCD Application Team
* @version V2.1.0
* @date 18-November-2011
* @brief Main program body
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm8s.h"
#include <stdio.h>
#include <math.h>
/* Private defines -----------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
#define LED_GPIO_PORT GPIOA
#define LED_GPIO_PIN GPIO_PIN_3
void Delay(uint16_t nCount)
{
/* Decrement nCount value */
while (nCount != 0)
{
nCount--;
}
}
uint8_t i=0;
/* Private defines -----------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void Init_GPIO(void)
{
GPIO_Init(LED_GPIO_PORT, LED_GPIO_PIN, GPIO_MODE_OUT_PP_LOW_FAST);
}
void Init_Timer1(void)
{
TIM1_TimeBaseInit(0,TIM1_COUNTERMODE_UP ,65535,0);
TIM1_EncoderInterfaceConfig(TIM1_ENCODERMODE_TI1, TIM1_ICPOLARITY_RISING, TIM1_ICPOLARITY_RISING);
TIM1_ARRPreloadConfig(ENABLE);
TIM1_Cmd(ENABLE);
}
void Init_Timer4(void)
{
/*TIM4_UpdateDisableConfig(ENABLE);//允许更新事件
TIM4_ARRPreloadConfig(ENABLE);//自动重装
TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);//中断配置,更新中断
TIM4_SetCounter(0xff);//计数器初值
TIM4_SetAutoreload(0xFF);//计数器自动重装的初值
TIM4_PrescalerCoL(TIM4_PRESCALER_128, TIM4_PSCRELOADMODE_UPDATE);//预分频值
*/
TIM4_TimeBaseInit(TIM4_PRESCALER_128, 0xff);
/* Clear TIM4 update flag */
TIM4_ClearFlag(TIM4_FLAG_UPDATE);
/* Enable update interrupt */
TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
TIM4_Cmd(ENABLE);
}
void Init_UART1(void)
{
UART1_DeInit();
UART1_Init((u32)9600, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO, UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TX_ENABLE);
//UART1_Cmd(ENABLE);
}
void Send(uint8_t dat)
{
while(( UART1_GetFlagStatus(UART1_FLAG_TXE)==RESET));
UART1_SendData8(dat);
}
void UART_SendString( char *p)
{
while(*p!=0x00) //判断字符串是否发送完毕
{
Send(*p++);
}
}
void sendswj_int (int datasend)//上位机发送格式
{
sprintf(send_int,"%d",abs(datasend));
if(datasend>0)
UART_SendString("+");
else
UART_SendString("-");
UART_SendString(send_int);
//UART_SendString(0,"b");
UART_SendString("\r\n");
}
void main(void)
{
int ecode=0;
int temp=0;
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
Init_UART1();
Init_GPIO();
// Init_Timer4();
Init_Timer1();
enableInterrupts();
while (1)
{
ecode=(TIM1->CNTRH<<8)|TIM1->CNTRL;
if(ecode!=temp)
sendswj_int(ecode);
temp=ecode;
}
}
#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 2011 STMicroelectronics *****END OF FILE****/
|