|
发表于 2013-8-16 13:30:07
|
显示全部楼层
smset 发表于 2013-8-16 13:21 ![](static/image/common/back.gif)
可以正常运行的。
我在stm8s103使用库,用到, 编绎正常,但程序不能正常运行,不知从何查起
-
- #include "ptask.h"
- #include "stm8s.h"
-
- #define LED_GPIO_PORT (GPIOA)
- #define LED_GPIO_PINS (GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3)
- #define SW_4 GPIO_PIN_1
- #define SW_5 GPIO_PIN_2
- #define LED_RUN_OR_WAIT GPIO_PIN_3
- #define LED_GPIO_PORT2 (GPIOC)
- #define LED_GPIO_PINS2 (GPIO_PIN_5 | GPIO_PIN_6)
- #define LED_WAIT GPIO_PIN_5
- #define LED_RUN3 GPIO_PIN_6
- #define LED_GPIO_PORT3 (GPIOD)
- #define LED_GPIO_PINS3 (GPIO_PIN_6)
- #define LED_RUN GPIO_PIN_6
-
- #define KEY_GPIO_PORT (GPIOD)
- #define KEY_GPIO_PINS (GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 )
- #define KEY_POWER GPIO_PIN_1
- #define KEY_REDUCE GPIO_PIN_2
- #define KEY_PLUS GPIO_PIN_3
- #define KEY_MODE_PINS (GPIO_PIN_4)
- #define KEY_MODE GPIO_PIN_4
- #define KEY_TRIGGER_PINS (GPIO_PIN_5)
- #define KEY_TRIGGER GPIO_PIN_5
- #define KEY_GPIO_PORT2 (GPIOC)
- #define KEY_SW_PINS (GPIO_PIN_7)
- #define KEY_SW GPIO_PIN_7
- #define MilliSec 1
- #define Sec 1000
- #define STEP 5 //约5%
- #define MAX 70 //100%
- #define MIN 15 //约21%
- #define OFF 0
- #define ON 1
- #define RUN 2
- #define WAIT 3
- #define TIM4_PERIOD 124 //1ms定时
-
- void GPIO_Configuration(void);
- void TIM4_Config(void);
- void TimingDelay_Decrement(void);
- void PWM1_Init(void);
- void SetTIM1_PWM_DutyCycle(uint16_t TIM1_Pulse);
- unsigned char Blink(void);
- unsigned char SwRead(void);
- void KeyRead(void);
- unsigned char Key_thread(void);
- void LightReduce(void);
- void LightPlus(void);
- void PowerEvent(void);
- void SwEvent(void);
- void Write_Byte (unsigned int address,unsigned char Data);
- unsigned char task0(void);
- unsigned char task1(void);
- /* Private functions -------------------------------------------*/
- /* Global variables --------------------------------------------*/
- volatile unsigned char timers[MAXTASKS]={0};
- unsigned char Trg;
- unsigned char Cont;
- unsigned char BlinkOfNumber = 0;
- unsigned char DutyCycle;
- unsigned char Mode;
- unsigned char ChangeFlag = 1;
- unsigned char SaveFlag;
- unsigned int SaveDelay;
- unsigned char RunF;
- unsigned char WaitF;
- unsigned char SwF;
- /**
- ****************************************************************
- * @brief Main function.
- * @par Parameters: None
- * @retval void None
- * @par Required preconditions: None
- ****************************************************************
- */
- void main(void)
- {
- /* Configures clocks Fmaster = 16MHz */
- CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
-
- /* read light value */
- DutyCycle = FLASH_ReadByte(FLASH_DATA_START_PHYSICAL_ADDRESS);
- if (! ((DutyCycle >= MIN) && (DutyCycle <= MAX)))
- { DutyCycle = (5 * MIN) ; }
-
- /* Configures GPIOs */
- GPIO_Configuration();
- /* init pb */
- SwF = FLASH_ReadByte(FLASH_DATA_START_PHYSICAL_ADDRESS+1);
- if (SwF)
- {
- GPIO_WriteHigh(LED_GPIO_PORT, SW_4);
- GPIO_WriteLow(LED_GPIO_PORT, SW_5);
- }
- else
- {
- GPIO_WriteLow(LED_GPIO_PORT, SW_4);
- GPIO_WriteHigh(LED_GPIO_PORT, SW_5);
- }
- /* read switch mode */
- if ( GPIO_ReadInputPin(KEY_GPIO_PORT, KEY_MODE))
- {Mode = ON;}
- else
- {Mode = OFF;}
- /* Initialize PWM */
- PWM1_Init();
- /* TIM4 configuration */
- TIM4_Config();
- /* enable interrupts */
- enableInterrupts();
- while(1)
- {
- /* 键盘处理 */
- RunTask(Key_thread,Task0_ID);
-
- /* 常闭键处理 */
- if (! GPIO_ReadInputPin(KEY_GPIO_PORT, KEY_TRIGGER))
- {
- if (Mode == RUN) { Mode = WAIT; ChangeFlag = 1; }
- }
- else
- {
- if (Mode == WAIT) { Mode = RUN; ChangeFlag = 1; }
- }
-
- /* 闪烁服务 */
- RunTask(Blink,Task1_ID);
- /* 模式服务 */
- if ((ChangeFlag == 1) && (BlinkOfNumber == 0))
- {
- ChangeFlag = 0;
- if (Mode == OFF)
- {
- GPIO_WriteLow(LED_GPIO_PORT3, LED_RUN);
- RunF = 0; WaitF = 0;
- GPIO_WriteLow(LED_GPIO_PORT2, LED_WAIT);
- GPIO_WriteLow(LED_GPIO_PORT2, LED_RUN3);
- TIM1_CtrlPWMOutputs(DISABLE);
- }
- if (Mode == ON) { Mode = RUN; }
- if (Mode == RUN)
- {
- GPIO_WriteHigh(LED_GPIO_PORT3, LED_RUN);
- RunF = 1; WaitF = 0;
- GPIO_WriteHigh(LED_GPIO_PORT2, LED_WAIT);
- GPIO_WriteHigh(LED_GPIO_PORT2, LED_RUN3);
- TIM1_CtrlPWMOutputs(ENABLE);
- }
- if (Mode == WAIT)
- {
- GPIO_WriteLow(LED_GPIO_PORT3, LED_RUN);
- RunF = 0; WaitF = 1;
- GPIO_WriteLow(LED_GPIO_PORT2, LED_WAIT);
- GPIO_WriteLow(LED_GPIO_PORT2, LED_RUN3);
- TIM1_CtrlPWMOutputs(DISABLE);
- }
-
- if (RunF || WaitF)
- { GPIO_WriteHigh(LED_GPIO_PORT, LED_RUN_OR_WAIT); }
- else
- { GPIO_WriteLow(LED_GPIO_PORT, LED_RUN_OR_WAIT);}
- }
-
-
- RunTask(SwRead,Task2_ID);
- /* 存储服务 */
- if (SaveFlag == 1)
- {
- if (SaveDelay == 0 )
- {
- Write_Byte(FLASH_DATA_START_PHYSICAL_ADDRESS,DutyCycle);
- Write_Byte(FLASH_DATA_START_PHYSICAL_ADDRESS+1,SwF);
- SaveFlag = 0;
- }
- }
-
- // RunTask(task0,Task1_ID);
- // RunTask(task1,Task2_ID);
- }
-
- }
- /**
- ******************************************************************
- * @brief Toggle Sw switch
- * @par Parameters: None
- * @retval void None
- * @par Required preconditions: None
- ******************************************************************
- */
- unsigned char SwRead(void)
- {
- _BEGIN
- if (GPIO_ReadInputPin(KEY_GPIO_PORT2, KEY_SW))
- {
- WaitX(20);
- if (GPIO_ReadInputPin(KEY_GPIO_PORT2, KEY_SW)) SwEvent();
- }
- _END
- }
- unsigned char task0(void)
- {
- _BEGIN
- while(1)
- {
- WaitX(100);
- GPIO_WriteHigh(LED_GPIO_PORT,SW_4);
- WaitX(200);
- GPIO_WriteLow(LED_GPIO_PORT,SW_4);
- }
- _END
- }
- unsigned char task1(void)
- {
- _BEGIN
- while(1)
- {
- WaitX(50);
- GPIO_WriteHigh(LED_GPIO_PORT,SW_5);
- WaitX(50);
- GPIO_WriteLow(LED_GPIO_PORT,SW_5);
- }
- _END
- }
- /**
- ******************************************************************
- * @brief Delay before completing the action
- * @param[in] function action() to be performed once the delay past
- * @param[in] None
- * @retval void None
- * @par Required preconditions: None
- ******************************************************************
- */
- unsigned char Key_thread(void)
- {
- _BEGIN
- KeyRead();
- if (Trg)
- {
- WaitX(20);
- KeyRead();
- if(Cont)
- {
- if ((Cont & KEY_PLUS) && (Mode == RUN)) { LightPlus(); }
- if ((Cont & KEY_REDUCE) && (Mode == RUN)) { LightReduce(); }
- if (Cont & KEY_POWER) { PowerEvent(); }
- }
- }
- _END
- }
- /**
- ******************************************************************
- * @brief Delay before completing the action
- * @param[in] function action() to be performed once the delay past
- * @param[in] None
- * @retval void None
- * @par Required preconditions: None
- ******************************************************************
- */
- unsigned char Blink(void)
- {
- _BEGIN
- if (BlinkOfNumber > 0)
- {
- WaitX(100);
- GPIO_WriteReverse(LED_GPIO_PORT, LED_WAIT);
- BlinkOfNumber--;
- }
- _END
- }
- /**
- ***************************************************************
- * @brief Configures GPIOs
- * @par Parameters: None
- * @retval void None
- * @par Required preconditions: None
- ***************************************************************
- */
- void GPIO_Configuration(void)
- {
- /* GPIOD reset */
- GPIO_DeInit(LED_GPIO_PORT);
- GPIO_DeInit(LED_GPIO_PORT2);
- GPIO_DeInit(KEY_GPIO_PORT);
-
- GPIO_Init(LED_GPIO_PORT, (GPIO_Pin_TypeDef)LED_GPIO_PINS,GPIO_MODE_OUT_PP_LOW_FAST);
- GPIO_Init(LED_GPIO_PORT2, (GPIO_Pin_TypeDef)LED_GPIO_PINS2,GPIO_MODE_OUT_PP_LOW_FAST);
- GPIO_Init(LED_GPIO_PORT3, (GPIO_Pin_TypeDef)LED_GPIO_PINS3,GPIO_MODE_OUT_PP_LOW_FAST);
-
- GPIO_Init(KEY_GPIO_PORT, (GPIO_Pin_TypeDef)KEY_GPIO_PINS,GPIO_MODE_IN_PU_NO_IT);
- GPIO_Init(KEY_GPIO_PORT, (GPIO_Pin_TypeDef)KEY_TRIGGER_PINS,GPIO_MODE_IN_PU_NO_IT);
- GPIO_Init(KEY_GPIO_PORT, (GPIO_Pin_TypeDef)KEY_MODE_PINS,GPIO_MODE_IN_PU_NO_IT);
- GPIO_Init(KEY_GPIO_PORT2, (GPIO_Pin_TypeDef)KEY_SW_PINS,GPIO_MODE_IN_PU_NO_IT);
-
- }
- /**
- *****************************************************************
- * @brief Configure TIM4 to generate an update interrupt each 1ms
- * @param None
- * @retval None
- *****************************************************************
- */
- void TIM4_Config(void)
- {
- /* TIM4 configuration:
- - TIM4CLK is set to 16 MHz, the TIM4 Prescaler is equal to 128 so the TIM1 counter
- clock used is 16 MHz / 128 = 125 000 Hz
- - With 125 000 Hz we can generate time base:
- max time base is 2.048 ms if TIM4_PERIOD = 255 --> (255 + 1) / 125000 = 2.048 ms
- min time base is 0.016 ms if TIM4_PERIOD = 1 --> ( 1 + 1) / 125000 = 0.016 ms
- - In this example we need to generate a time base equal to 1 ms
- so TIM4_PERIOD = (0.001 * 125000 - 1) = 124 */
- /* Time base configuration */
- TIM4_TimeBaseInit(TIM4_PRESCALER_128, TIM4_PERIOD);
- /* Clear TIM4 update flag */
- TIM4_ClearFlag(TIM4_FLAG_UPDATE);
- /* Enable update interrupt */
- TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
- /* Enable TIM4 */
- TIM4_Cmd(ENABLE);
- }
- /**
- ********************************************************
- * @brief Decrements the TimingDelay variable.
- * @param None
- * @retval None
- ********************************************************
- */
- void TimingDelay_Decrement(void)
- { if (SaveDelay != 0x00) { SaveDelay--;} }
- /**
- *****************************************************************
- * @brief Initialize all the PWM Parameters
- * @par Parameters: None
- * @retval void None
- * @par Required preconditions: None
- *****************************************************************
- */
- void PWM1_Init(void)
- {
- /* PWM1 Mode configuration Channel2
- * Duty Cycle = [TIM1_CCR2/(TIM1_ARR+1)]*100=50% */
-
- TIM1_DeInit();
-
- /* Time Base configuration */
- /*
- TIM1_Prescaler = 1280 分频16/1280 = 12.5 KHz
- TIM1_Period = 69 频率12.5K /(69+1) = 179Hz
- TIM1_CounterMode = TIM1_COUNTERMODE_UP
- TIM1_RepetitionCounter = 0
- */
- TIM1_TimeBaseInit(1280, TIM1_COUNTERMODE_UP, 69, 0);
-
- /* Channel 2 Configuration in PWM mode */
- /*
- TIM1_OCMode = TIM1_OCMODE_PWM2
- TIM1_OutputState = TIM1_OUTPUTSTATE_ENABLE
- TIM1_OutputNState = TIM1_OUTPUTNSTATE_ENABLE
- TIM1_Pulse = DutyCycle
- TIM1_OCPolarity = TIM1_OCPOLARITY_LOW
- TIM1_OCNPolarity = TIM1_OCNPOLARITY_HIGH
- TIM1_OCIdleState = TIM1_OCIDLESTATE_SET
- TIM1_OCNIdleState = TIM1_OCIDLESTATE_RESET
- */
- TIM1_OC3Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE, DutyCycle,
- TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET,
- TIM1_OCNIDLESTATE_RESET);
-
- TIM1_OC4Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, DutyCycle, TIM1_OCPOLARITY_LOW,
- TIM1_OCIDLESTATE_SET);
-
- /* TIM1 counter enable */
- TIM1_Cmd(ENABLE);
-
- /* TIM1 Main Output Enable */
- TIM1_CtrlPWMOutputs(ENABLE);
- }
- /**
- ***************************************************************
- * @brief Set DutyCycle value
- * @par Parameters: None
- * @retval void None
- * @par Required preconditions: None
- ***************************************************************
- */
- void SetTIM1_PWM_DutyCycle(uint16_t TIM1_Pulse)
- {
- /* Set the Pulse value */
- TIM1->CCR3H = (uint8_t)(TIM1_Pulse >> 8);
- TIM1->CCR3L = (uint8_t)(TIM1_Pulse);
- /* Set the Pulse value */
- TIM1->CCR4H = (uint8_t)(TIM1_Pulse >> 8);
- TIM1->CCR4L = (uint8_t)(TIM1_Pulse);
- }
- /**
- ******************************************************************
- * @brief Toggle Power switch
- * @par Parameters: None
- * @retval void None
- * @par Required preconditions: None
- ******************************************************************
- */
- void KeyRead( void )
- {
- //uint8_t ReadData = (GPIO_ReadInputData(KEY_GPIO_PORT)^0xFF) & 0x0E ;
- unsigned char ReadData = (GPIO_ReadInputData(KEY_GPIO_PORT)^0xFF) & 0x0E ;
- Trg = ReadData & (ReadData ^ Cont);
- Cont = ReadData;
- }
- /**
- ******************************************************************
- * @brief Toggle Power switch
- * @par Parameters: None
- * @retval void None
- * @par Required preconditions: None
- ******************************************************************
- */
- void PowerEvent(void)
- {
- if ( Mode == OFF ) { Mode = ON; }
- else { Mode = OFF; }
- ChangeFlag = 1;
- }
- /**
- ****************************************************************
- * @brief Configures clocks
- * @par Parameters: None
- * @retval void None
- * @par Required preconditions: None
- ****************************************************************
- */
- void LightPlus(void)
- {
- if (DutyCycle == MAX)
- {
- if (BlinkOfNumber == 0) {BlinkOfNumber = 16;}
- }
- else
- {
- DutyCycle = (DutyCycle + STEP);
- SetTIM1_PWM_DutyCycle(DutyCycle);
- SaveFlag = 1;
- SaveDelay = (60 * Sec);
- }
- }
- /**
- ****************************************************************
- * @brief Configures clocks
- * @par Parameters: None
- * @retval void None
- * @par Required preconditions: None
- ****************************************************************
- */
- void LightReduce(void)
- {
- if (DutyCycle == MIN)
- {
- if (BlinkOfNumber == 0 ) {BlinkOfNumber = 16;}
- }
- else
- {
- DutyCycle = (DutyCycle - STEP);
- SetTIM1_PWM_DutyCycle(DutyCycle);
- SaveFlag = 1;
- SaveDelay = (60 * Sec);
- }
- }
- /**
- ******************************************************************
- * @brief Toggle Sw switch
- * @par Parameters: None
- * @retval void None
- * @par Required preconditions: None
- ******************************************************************
- */
- void SwEvent(void)
- {
- if ( Mode == ON )
- {
- GPIO_WriteReverse(LED_GPIO_PORT, SW_4);
- GPIO_WriteReverse(LED_GPIO_PORT, SW_5);
- SwF = GPIO_ReadInputPin(LED_GPIO_PORT, SW_4);
- SaveFlag = 1;
- SaveDelay = (60 * Sec);
- }
- }
- /**
- ******************************************************************
- * @brief Write EEPROM by byte
- * @param[address] Start address
- * @param[Data] Data
- * @retval void None
- ******************************************************************
- */
- void Write_Byte (unsigned int address,unsigned char Data)
- {
- FLASH->DUKR = 0xAE;
- FLASH->DUKR = 0x56;
- FLASH->CR2 = 0x00;
- FLASH->NCR2 = 0xFF;
- *((u8*)address) = Data;
- FLASH->IAPSR = (u8)(~0x08);
- }
- #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(uint8_t* file, uint32_t 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
复制代码 |
|