#include <stdio.h>
#include <stdlib.h>
char * get(int i){
static char str1[]="b ";
static char str2[]="a ";
static char str3[]="ptr1";
static char str4[]="ptr2";
switch(i){
case 0:
return str4;
case 1:
return str3;
case 2:
return str1;
case 3:
return str2;
default:
return "known";}
}
void getaddress(int ptr, int * ptr3){
for (int i=0;i<4;i++){
ptr3=(int*)(ptr+4*i);
printf("%s =%p, * in hex =%08X, * in dec =%08d\n",get(i), ptr3, *ptr3, *ptr3);
}
printf("\n");
}
int main(void){
int i=0,a,b;
int *ptr1, *ptr2,*ptr3;
int ptr = (int)&ptr2;
printf("&i=%p\n",&i);
printf("&a=%p\n",&a);
printf("&b=%p\n",&b);
printf("&ptr1=%p\n",&ptr1);
printf("&ptr2=%p\n",&ptr2);
getaddress(ptr, ptr3);
a=5,b=10;
printf("a=5,b=10;\n");
getaddress(ptr, ptr3);
ptr1 = &a; ptr2 = &b;
printf("ptr1 = &a; ptr2 = &b;\n");
getaddress(ptr, ptr3);
*ptr1=7; *ptr2=32; //a=7, b=32
printf("*ptr1=7; *ptr2=32;\n");
getaddress(ptr, ptr3);
a=17; //a=17, b=32
printf("a=17;\n");
getaddress(ptr, ptr3);
ptr1=ptr2; //ptr1=&b
printf("ptr1=ptr2;\n");
getaddress(ptr, ptr3);
*ptr1=9; //b=9, a=17
printf("*ptr1=9;\n");
getaddress(ptr, ptr3);
ptr1=&a; //ptr1=&a
printf("ptr2=&a;\n");
getaddress(ptr, ptr3);
a=64; //a=64, b=9, ptr1=&a, ptr2=&b
printf("ptr2=&a;\n");
getaddress(ptr, ptr3);
*ptr2=*ptr1+5; // b=a+5=64+5=69
printf("ptr2=&a;\n");
getaddress(ptr, ptr3);
ptr2=&a; //ptr2=&a, ptr1=&a
printf("ptr2=&a;\n");
getaddress(ptr, ptr3);
printf("a=%2d, b=%2d, *ptr1=%2d, *ptr2=%2d\n", a, b, *ptr1, *ptr2);
printf(" ptr1=%p, ptr2=%p\n", ptr1, ptr2);
printf(" ptr1=%p, &*ptr1=%p\n", ptr1, &*ptr1);
ptr1=(int*)0x28FEA0;
printf(" ptr1=%p, *ptr1=%d\n", ptr1, *ptr1);
system("pause");
return 0;
}
結果
&i=0028FEB4
&a=0028FEB0
&b=0028FEAC
&ptr1=0028FEA8
&ptr2=0028FEA4
ptr2 =0028FEA4, * in hex =00405000, * in dec =04214784
ptr1 =0028FEA8, * in hex =0028FEC8, * in dec =02686664
b =0028FEAC, * in hex =004021DB, * in dec =04202971
a =0028FEB0, * in hex =00402180, * in dec =04202880
a=5,b=10;
ptr2 =0028FEA4, * in hex =00405000, * in dec =04214784
ptr1 =0028FEA8, * in hex =0028FEC8, * in dec =02686664
b =0028FEAC, * in hex =0000000A, * in dec =00000010
a =0028FEB0, * in hex =00000005, * in dec =00000005
ptr1 = &a; ptr2 = &b;
ptr2 =0028FEA4, * in hex =0028FEAC, * in dec =02686636
ptr1 =0028FEA8, * in hex =0028FEB0, * in dec =02686640
b =0028FEAC, * in hex =0000000A, * in dec =00000010
a =0028FEB0, * in hex =00000005, * in dec =00000005
*ptr1=7; *ptr2=32;
ptr2 =0028FEA4, * in hex =0028FEAC, * in dec =02686636
ptr1 =0028FEA8, * in hex =0028FEB0, * in dec =02686640
b =0028FEAC, * in hex =00000020, * in dec =00000032
a =0028FEB0, * in hex =00000007, * in dec =00000007
a=17;
ptr2 =0028FEA4, * in hex =0028FEAC, * in dec =02686636
ptr1 =0028FEA8, * in hex =0028FEB0, * in dec =02686640
b =0028FEAC, * in hex =00000020, * in dec =00000032
a =0028FEB0, * in hex =00000011, * in dec =00000017
ptr1=ptr2;
ptr2 =0028FEA4, * in hex =0028FEAC, * in dec =02686636
ptr1 =0028FEA8, * in hex =0028FEAC, * in dec =02686636
b =0028FEAC, * in hex =00000020, * in dec =00000032
a =0028FEB0, * in hex =00000011, * in dec =00000017
*ptr1=9;
ptr2 =0028FEA4, * in hex =0028FEAC, * in dec =02686636
ptr1 =0028FEA8, * in hex =0028FEAC, * in dec =02686636
b =0028FEAC, * in hex =00000009, * in dec =00000009
a =0028FEB0, * in hex =00000011, * in dec =00000017
ptr2=&a;
ptr2 =0028FEA4, * in hex =0028FEAC, * in dec =02686636
ptr1 =0028FEA8, * in hex =0028FEB0, * in dec =02686640
b =0028FEAC, * in hex =00000009, * in dec =00000009
a =0028FEB0, * in hex =00000011, * in dec =00000017
ptr2=&a;
ptr2 =0028FEA4, * in hex =0028FEAC, * in dec =02686636
ptr1 =0028FEA8, * in hex =0028FEB0, * in dec =02686640
b =0028FEAC, * in hex =00000009, * in dec =00000009
a =0028FEB0, * in hex =00000040, * in dec =00000064
ptr2=&a;
ptr2 =0028FEA4, * in hex =0028FEAC, * in dec =02686636
ptr1 =0028FEA8, * in hex =0028FEB0, * in dec =02686640
b =0028FEAC, * in hex =00000045, * in dec =00000069
a =0028FEB0, * in hex =00000040, * in dec =00000064
ptr2=&a;
ptr2 =0028FEA4, * in hex =0028FEB0, * in dec =02686640
ptr1 =0028FEA8, * in hex =0028FEB0, * in dec =02686640
b =0028FEAC, * in hex =00000045, * in dec =00000069
a =0028FEB0, * in hex =00000040, * in dec =00000064
a=64, b=69, *ptr1=64, *ptr2=64
ptr1=0028FEB0, ptr2=0028FEB0
ptr1=0028FEB0, &*ptr1=0028FEB0
ptr1=0028FEA0, *ptr1=48
請按任意鍵繼續 . . .
2018年4月26日 星期四
2018年4月25日 星期三
Using FreeRTOS multi-tasking in Arduino
https://www.hackster.io/feilipu/using-freertos-multi-tasking-in-arduino-ebc3cc
#include <Arduino_FreeRTOS.h>
#include <croutine.h>
#include <event_groups.h>
#include <FreeRTOSConfig.h>
#include <FreeRTOSVariant.h>
#include <list.h>
//#include <message_buffer.h>
#include <mpu_wrappers.h>
#include <portable.h>
#include <portmacro.h>
#include <projdefs.h>
#include <queue.h>
#include <semphr.h>
#include <stack_macros.h>
//#include <stream_buffer.h>
#include <task.h>
#include <timers.h>
void setup() {
// Now set up two tasks to run independently.
xTaskCreate(
TaskBlink
, (const portCHAR *)"Blink" // A name just for humans
, 128 // Stack size
, NULL
, 2 // priority
, NULL );
xTaskCreate(
TaskAnalogRead
, (const portCHAR *) "AnalogRead"
, 128 // This stack size can be checked & adjusted by reading Highwater
, NULL
, 1 // priority
, NULL );
xTaskCreate(
TaskBlink2
, (const portCHAR *)"Blink" // A name just for humans
, 128 // Stack size
, NULL
, 2 // priority
, NULL );
// Now the task scheduler, which takes over control of scheduling individual tasks, is automatically started.
}
void loop()
{
// Empty. Things are done in Tasks.
}
/*--------------------------------------------------*/
/*---------------------- Tasks ---------------------*/
/*--------------------------------------------------*/
void TaskBlink(void *pvParameters) // This is a task.
{
(void) pvParameters;
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
for (;;) // A Task shall never return or exit.
{
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
}
}
void TaskBlink2(void *pvParameters) // This is a task.
{
(void) pvParameters;
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
pinMode(9, INPUT);
for (;;) // A Task shall never return or exit.
{
if(digitalRead(9)){
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
vTaskDelay( 200 / portTICK_PERIOD_MS ); // wait for one second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
vTaskDelay( 200 / portTICK_PERIOD_MS ); // wait for one second
}
}
}
void TaskAnalogRead(void *pvParameters) // This is a task.
{
(void) pvParameters;
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
for (;;)
{
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
vTaskDelay(200/portTICK_PERIOD_MS); // one tick delay (15ms) in between reads for stability
}
}
訂閱:
文章 (Atom)