本帖最后由 ningling_21 于 2020-7-1 22:16 编辑
- #include <rtthread.h>
- #include <rtdevice.h>
- #include "HAL_device.h"
- #include "drv_uart.h"
- //#include "can.h"
- #include "HAL_can.h"
- /* defined the LED pin: PA15 */
- #define LED_PIN 38
- #define LED2_PIN 39
- #define LED3_PIN 40
- #define LED4_PIN 41
- static rt_uint8_t led_1_stack[128];
- static struct rt_thread led_1_thread;
- static rt_uint8_t led_2_stack[128];
- static struct rt_thread led_2_thread;
- //--------------------------------------------------------------------------------------------------
- static void led_1_thread_entry(void * parameter)//线程1入口
- {
- u8 led_stat_led=0;
- //rt_hw_led_init();
- //rt_hw_usart_init();
- rt_pin_mode(LED4_PIN, PIN_MODE_OUTPUT);
-
- while(1)
- {
-
- if(led_stat_led==0)
- {
- led_stat_led=1;
-
- rt_pin_write(LED4_PIN, PIN_LOW);
- }
- else
- {
- led_stat_led=0;
-
- rt_pin_write(LED4_PIN, PIN_HIGH);
-
- //mm32_uart_putc(UART1,0x55);
- }
- rt_thread_delay( 200 );
- }
- }
- static void led_2_thread_entry(void * parameter)//线程2入口
- {
- u8 led_stat_led=0;
- //rt_hw_led_init();
- //rt_hw_usart_init();
- rt_pin_mode(LED3_PIN, PIN_MODE_OUTPUT);
- while(1)
- {
- if(led_stat_led==0)
- {
- led_stat_led=1;
-
- rt_pin_write(LED3_PIN, PIN_LOW);
- }
- else
- {
- led_stat_led=0;
-
- rt_pin_write(LED3_PIN, PIN_HIGH);
- }
- rt_thread_delay( 100 );
- }
- }
- int main(void)
- {
- u16 run_cnt=0;
- u8 led_stat_led=0;
- u8 flag_rt_thread=0;
- rt_err_t result;
- int count = 1;
- /* set LED4 pin mode to output */
- rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT);
- rt_pin_mode(LED2_PIN, PIN_MODE_OUTPUT);
- //rt_pin_mode(LED3_PIN, PIN_MODE_OUTPUT);
- rt_hw_uart_init();
- while (count)
- {
- run_cnt++;
- if(led_stat_led==0)
- {
- led_stat_led=1;
- rt_pin_write(LED_PIN, PIN_HIGH);
- rt_pin_write(LED2_PIN, PIN_LOW);
- //rt_pin_write(LED3_PIN, PIN_HIGH);
- //rt_pin_write(LED4_PIN, PIN_LOW);
- }
- else
- {
- led_stat_led=0;
- rt_pin_write(LED_PIN, PIN_LOW);
- rt_pin_write(LED2_PIN, PIN_HIGH);
- //rt_pin_write(LED3_PIN, PIN_LOW);
- //rt_pin_write(LED4_PIN, PIN_HIGH);
- }
-
- rt_thread_mdelay(100);
-
- //rt_kprintf("run_count=%d\r\n",run_cnt);
- if(run_cnt>=20)
- {
- run_cnt=0;
- if(flag_rt_thread==0)
- {
- flag_rt_thread=1;
- //rt_thread_create()
- result = rt_thread_init(&led_1_thread,
- "led",
- led_1_thread_entry,
- RT_NULL,
- (rt_uint8_t*)&led_1_stack[0], //堆栈起始
- sizeof(led_1_stack), //堆栈大小
- 21, //线程优先级
- 10); //时间片数
-
- if (result == RT_EOK)
- {
- rt_thread_startup(&led_1_thread);
- }
- rt_kprintf("led1_thread ok.\n");
-
- result = rt_thread_init(&led_2_thread,
- "led",
- led_2_thread_entry,
- RT_NULL,
- (rt_uint8_t*)&led_2_stack[0], //堆栈起始
- sizeof(led_2_stack), //堆栈大小
- 22, //线程优先级
- 5); //时间片数
-
- if (result == RT_EOK)
- {
- rt_thread_startup(&led_2_thread);
- }
- }
-
-
- }
- }
- return RT_EOK;
- }
|