Table of Contents
Portwell NANO-6050
Portwell Engineering Tool
GPIO Use Examples
LED Blink
Connect LEDs (though resistors!) to GPIO 1 & 2
- blink.c
#include "stdafx.h" #include "User_Api.h" #include "PET_Type.h" #include "time.h" void delay(int sec) { int ms = 1000 * sec; clock_t start_time = clock(); while (clock() < start_time + ms); } int main() { int i; // init portwell API PET_API_Init(); // set GPIO 1 and 2 to outputs PET_GPIO_SetPinDirection (1, 1, 0); PET_GPIO_SetPinDirection (1, 2, 0); // blink! for (i = 1; i < 100; i++) { PET_GPIO_WritePin(1, 1, 1); PET_GPIO_WritePin(1, 2, 0); delay(75); PET_GPIO_WritePin(1, 1, 0); PET_GPIO_WritePin(1, 2, 1); delay(75); } // turn both LEDs off PET_GPIO_WritePin(1, 1, 0); PET_GPIO_WritePin(1, 2, 0); // close portwell API PET_API_Uninit (); }
Build:
gcc blink.c -o blink libapi_x64.a -lm
Input Test
Connect GPIOs 3 & 4 to ground through buttons
- inputTest.c
#include "stdafx.h" #include "User_Api.h" #include "PET_Type.h" #include "time.h" void delay(int sec) { int ms = 1000 * sec; clock_t start_time = clock(); while (clock() < start_time + ms); } int main() { int i; // start portwell API PET_API_Init(); // set pin 3 & 4 as an input PET_GPIO_SetPinDirection (1, 3, 1); PET_GPIO_SetPinDirection (1, 4, 1); unsigned char greenvalue = 0; unsigned char redvalue = 0; for (i = 1; i < 20; i++) { // read pins, print values PET_GPIO_ReadPin(1, 4, &greenvalue); printf("\n Green is: %d", greenvalue); PET_GPIO_ReadPin(1, 3, &redvalue); printf("\n Red is: %d\n", redvalue); delay(200); } printf("\n"); // close API PET_API_Uninit (); }
Build:
gcc inputTest.c -o inputTest libapi_x64.a -lm
Button LED
Lights LEDs when buttons are pressed.
- buttonLED.c
#include "stdafx.h" #include "signal.h" #include "User_Api.h" #include "PET_Type.h" #include "time.h" static volatile sig_atomic_t keep_running = 1; static void sig_handler(int _) { (void)_; keep_running = 0; } int main(void) { signal(SIGINT, sig_handler); // start portwell API PET_API_Init(); // set pin 3 & 4 as inputs PET_GPIO_SetPinDirection (1, 3, 1); PET_GPIO_SetPinDirection (1, 4, 1); // set pin 1 & 2 as outputs PET_GPIO_SetPinDirection (1, 1, 0); PET_GPIO_SetPinDirection (1, 2, 0); // pull 1 & 2 low PET_GPIO_WritePin(1, 1, 0); PET_GPIO_WritePin(1, 2, 0); unsigned char greenvalue = 0; unsigned char redvalue = 0; while (keep_running) { // read pins, print values PET_GPIO_ReadPin(1, 4, &greenvalue); PET_GPIO_ReadPin(1, 3, &redvalue); // check for button presses (0) // bring pin high if pressed, low when released // green if (greenvalue == 0) { PET_GPIO_WritePin(1, 2, 1); } else { if (greenvalue == 1) { PET_GPIO_WritePin(1, 2, 0); } //red if (redvalue == 0) { PET_GPIO_WritePin(1, 1, 1); } else { if (redvalue == 1) { PET_GPIO_WritePin(1, 1, 0); } } } } // close API PET_API_Uninit (); return EXIT_SUCCESS; }
Heartbeat
- heartbeat.c
#include "stdafx.h" #include "signal.h" #include "User_Api.h" #include "PET_Type.h" #include "time.h" #include "stdio.h" static volatile sig_atomic_t keep_running = 1; static void sig_handler(int _) { (void)_; keep_running = 0; } void delay(int sec) { int ms = 1000 * sec; clock_t start_time = clock(); while (clock() < start_time + ms); } int main(void) { signal(SIGINT, sig_handler); // init portwell API PET_API_Init(); // set GPIO 1 and 2 to outputs PET_GPIO_SetPinDirection (1, 2, 0); // blink! while (keep_running) { PET_GPIO_WritePin(1, 2, 1); delay(75); PET_GPIO_WritePin(1, 2, 0); delay(100); PET_GPIO_WritePin(1, 2, 1); delay(75); PET_GPIO_WritePin(1, 2, 0); //delay(500); sleep(2); } // turn both LEDs off PET_GPIO_WritePin(1, 2, 0); // close portwell API PET_API_Uninit (); return EXIT_SUCCESS; }