Trying to create a reaction timer. Timer rand counts down from 10. LED is red and turns green when the user has pressed the switch. The user has to press either sw2 or sw3 when prompted has a 5 second gap to press switch. Once switch is pressed, the time is recorded and printed to putty. Game then resets and user can play again.
I'm current stuck trying to implement the LED and rand timer, could anyone help:
#include "freeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "queue.h"
#include "timers.h"
#include "event_groups.h"
#include <stdio.h>
#include "board.h"
#include "peripherals.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "MK64F12.h"
#include "fsl_debug_console.h"
#include "fsl_UART.h"
#include "fsl_device_registers.h"
/* TODO: insert other definitions and declarations here. */
QueueHandle_t switchQueue = NULL;
TaskHandle_t displayHandle = NULL, UARTHandle =NULL, LED_TaskHandle = NULL;
static void displayTask(void * pvParameters);
static void LED_Task(void *pvParameters);
static EventGroupHandle_t SW_Group = NULL;
#define SW2_bit 1<<0
#define SW3_bit 1<<1
/*
* #brief Application entry point.
*/
void BOARD_SW2_IRQ_HANDLER(){
static int8_t count = 0;
static uint32_t buffer[5];
BaseType_t xHigherPriorityTaskWoken;
PORT_ClearPinsInterruptFlags(BOARD_SW2_PORT, 1<<BOARD_SW2_GPIO_PIN);
buffer[count] = xTaskGetTickCountFromISR()*(1000/configTICK_RATE_HZ);
printf("\n\rSW2 pressed at %d", buffer[count]);
/* if(GPIO_PinRead(BOARD_INITPINS_SW2_GPIO,BOARD_INITPINS_SW2_PIN)== 0)
{
PRINTF("Switch pressed!.\r\n");
xEventGroupSetBits(SW_Group, SW2_bit);
while(GPIO_PinRead(BOARD_INITPINS_SW2_GPIO,BOARD_INITPINS_SW2_PIN) == 0){}
//vTaskSuspend(NULL);
if(++count==5){
count=0;
xHigherPriorityTaskWoken = pdFALSE;
//send copy of buffer to queue
xQueueSendFromISR(switchQueue, buffer, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
}*/
vTaskDelay(pdMS_TO_TICKS(100));
}
int main(void) {
/* Init board hardware. */
BOARD_InitBootPins();
BOARD_InitBootClocks();
BOARD_InitBootPeripherals();
/* Init FSL debug console. */
BOARD_InitDebugConsole();
BOARD_InitLEDsPins();
//configure sw2 interrupt
NVIC_SetPriority(BOARD_SW2_IRQ, 10);
NVIC_ClearPendingIRQ(BOARD_SW2_IRQ);
NVIC_EnableIRQ(BOARD_SW2_IRQ);
SW_Group = xEventGroupCreate();
switchQueue = xQueueCreate(5,20); //5 item queue of 20 bytes per item
if(xTaskCreate(displayTask, "Time Display", 100, NULL, 3, &displayHandle)== pdFALSE)
{
printf("UART task not created\n\r");
while(1)
;
}
if (xTaskCreate(LED_Task, "LED_Task", configMINIMAL_STACK_SIZE + 10, NULL, 4, &LED_TaskHandle) != pdPASS)
{
PRINTF("LED_Task creation failed!.\r\n");
while (1)
;
}
vTaskStartScheduler();
volatile static int i = 0;
/* Enter an infinite loop, just incrementing a counter. */
while(1){
i++;
}
return 0 ;
}
static void displayTask(void * pvParameters){
uint32_t rxBuffer[5];
printf("Starting display task\n\r");
while(1){
if(xQueueReceive(switchQueue, &rxBuffer, portMAX_DELAY) == pdTRUE){
//print data
printf("\n\n\rData Received from Queue:");
int x;
for(x=0;x<5;x++){
printf("\n\rData %d: %d",x,rxBuffer[x]);
}
vTaskDelay(pdMS_TO_TICKS(100));
}
}
}
void LED_Task(void *pvParameters)
{
EventBits_t event_bits;
for (;;) //no conditions in the for loop, will run forever
{
event_bits = xEventGroupWaitBits(SW_Group, SW2_bit | SW3_bit, pdTRUE, pdFALSE, portMAX_DELAY);
if(event_bits & SW2_bit){
PRINTF("Event bit set, RED LED Toggled!.\r\n");
LED_RED_TOGGLE();
}
else if(event_bits & SW3_bit){
PRINTF("Event bit set, GREEN LED Toggled!.\r\n");
LED_GREEN_TOGGLE();
}
}
}
Related
The below code is to generate a single line signal with two different timings, doing good if we give more than 10ms in vTaskDelay(), i.e getting perfect delay is observed but if we give lesser than 10ms the perfect signal is not observed. Please help me in this regard.
#include <stdio.h>
#include "esp_timer.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "driver/gpio.h"
#include <stdbool.h>
#include "esp_err.h"
#include "sdkconfig.h"
#include "driver/timer.h"
#define GPIO_OUTPUT_IO 2
#define TIMER_GROUP 0
#define TIMER_INDEX 0
uint64_t task_counter_value;
void turn_off(void *pvParameters);
void turn_on(void *pvParameters);
void turn_on(void *pvParameters)
{
gpio_set_level(GPIO_OUTPUT_IO, 1);
vTaskDelay(pdMS_TO_TICKS(30));
xTaskCreate(&turn_off, "turn_off", 2048, NULL, 10, NULL);
vTaskDelete(NULL);
}
void turn_off(void *pvParameters)
{
gpio_set_level(GPIO_OUTPUT_IO, 0);
vTaskDelay(pdMS_TO_TICKS(8));
xTaskCreate(&turn_on, "turn_on", 2048, NULL, 10, NULL);
vTaskDelete(NULL);
}
void app_main(void)
{
timer_config_t config;
config.alarm_en = false;
config.auto_reload = true;
config.counter_dir = TIMER_COUNT_UP;
config.intr_type = TIMER_INTR_LEVEL;
config.counter_en = TIMER_PAUSE;
config.divider = 80;
gpio_config_t io_conf;
io_conf.intr_type = GPIO_INTR_DISABLE;
io_conf.mode = GPIO_MODE_OUTPUT;
io_conf.pin_bit_mask = (1ULL << GPIO_OUTPUT_IO);
io_conf.pull_down_en = 0;
io_conf.pull_up_en = 0;
gpio_config(&io_conf);
timer_init(TIMER_GROUP, TIMER_INDEX, &config);
xTaskCreate(&turn_on, "turn_on", 2048, NULL, 10, NULL);
while (true)
{
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
I have configured for the ADC channel AD0.1 of processor LPC 2148, with ISR. But while ADC value changes ISR is not executing. Because of this VICVectAddr contains the value of ISR function. This program is part of learning.
Please find the Code below. It is very help full if anybody has given solution
#include <lpc214x.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define SBIT_RESULT 6u
#define SBIT_CLCKDIV 8u
#define SBIT_BURST 16u
#define SBIT_START 24u
#define SBIT_PDN 21u
#define SBIT_EDGE 27u
#define SBIT_DONE 31u
void ADC_Init (void);
__irq void ADC_ISR (void) ;
void main(void) {
AD0CR=0x0;
ADC_Init ();
while(1){
}
}
void ADC_Init (void)
{
PINSEL1 = 0x01000000 ; // ADC P0.28, AD0.1
AD0INTEN =0x02; //On completetion of ADC channel 1 generate interupt
VICVectAddr5 = (unsigned int)ADC_ISR; // address of the ISR
VICVectCntl5 = (1<<5) | 18;
VICIntSelect &= ~ (1<<18);
VICIntEnable = (1<<18);
AD0CR = 0X00200D02;
AD0CR |= 0x01000000;
}
// Interrupt Service Routine-ISR
__irq void ADC_ISR(void) {
static int flag=0 ;
int val;
unsigned int data;
bool readStatus;
data = AD0GDR ;
readStatus = data & 0x80000000;
if(readStatus){
val = data >>=6;
val= val & 0x3FF;
val=((val/1023.0)*3.3);
}
Delay(1000000);
AD0INTEN =0x0; //clear ADC interrupt
VICVectAddr = 0;
}
```
Noob question:
I'm trying to learn about UART on an ARDUINO. I wrote some very simple code and for some reason, i can't make the receive() function work. I don't think it fetches the data from the UDR register. I'm using a small OLED display and i want to print the received data to it. No data is being printed to the display, when i run the code.
I connected the ports TX1 and RX1 with a wire on the board.
I tried finding youtube videos and have been reading alot. Appearently not enough.
Any ARDUINO expert who knows what to do?
#include <avr/io.h>
#include "ssd1306.h"
#include "I2C.h"
#include <stdio.h>
#include <util/delay.h>
void initOLED();
void initUART1();
void receive();
void transmit();
int main(void)
{
I2C_Init();
initOLED(); //initialiaze OLED
while (1)
{
transmit();
receive();
}
}
void initOLED()
{
I2C_Init();
InitializeDisplay();
clear_display();
}
void initUART1(void)
{
DDRD = 0x08; //TXD1 set to output
UBRR1L = 51; //Baudrate 19200
UBRR1H = 0; //upper four bits of baudrate
UCSR1A = 0x02; //Double speed mode
UCSR1B = 0x18; //Enable receive and transmit
UCSR1C = 0x06; //1 stop bit, 8-bit, no parity
}
void transmit()
{
char b = 'a';
while(!(UCSR1A & (1<<UDRE1))); //wait for an empty UDR register
UDR1 = b; //load character in register
}
void receive()
{
int Y = 0; //Y coordinate for cursor on the display
char d;
while(!(UCSR1A & (1<<RXC1))); //wait for unread data in the receive buffer
d = UDR1; //set UDR register in character d
sendCharXY(d, 1, Y); //send character to display
}
It looks like you never call initUART1().
I'm unable to break out of an if statement after a button has been depressed. The button activates a relay and the depression turns the relay off.
Here is my full code: I'm new to C so all comments and suggestions are much appreciated.
#include "mcc_generated_files/mcc.h"
#define FCY 8000000UL
#include <libpic30.h>
//#define baudrate 19200
int main(void)
{
// Initialise the device
SYSTEM_Initialize();
while(1)
{
LED4_SetLow(); // Turn on 3v3 LED
LED12v_SetHigh();
LED3_SetHigh();
LED1_SetHigh();
int button;
button = UART5_Read();
if (button == 0x08)
{
RLY1_SetHigh();
RLY3_SetHigh();
if (button == 0x00)
goto finished;
}
finished:
RLY1_SetLow();
RLY3_SetLow();
}
return (0);
}
I have now edited the code as follows. The relays are able to be set high but the condition 0x00 is never reached. Can someone please explain?
#include "mcc_generated_files/mcc.h"
#define FCY 8000000UL
#include <libpic30.h>
//#define baudrate 19200
int main(void)
{
// Initialise the device
SYSTEM_Initialize();
while(1)
{
LED4_SetLow(); // Turn on 3v3 LED
LED12v_SetHigh();
LED3_SetHigh();
LED1_SetHigh();
int button;
int release;
button = UART5_Read();
release = UART5_Read();
if (button == 0x08)
{
RLY1_SetHigh();
RLY3_SetHigh();
}
if (release == 0x00)
{
RLY1_SetLow();
RLY3_SetLow();
}
__delay_ms(1000);
}
return (0);
}
I haven't used this MCU nor its libs, but I believe correct code should look something along the lines of this:
#include "mcc_generated_files/mcc.h"
#include <libpic30.h>
#include <stdint.h>
#define BUTTON_CODE 0x8u // use named constants instead of magic numbers
void main (void)
{
// Initialise the device
SYSTEM_Initialize();
for(;;)
{
uint8_t data;
LED4_SetLow(); // Turn on 3v3 LED
LED12v_SetHigh();
LED3_SetHigh();
LED1_SetHigh();
if(UART5_Data_Ready())
{
data = UART5_Read();
if(data == BUTTON_CODE)
{
RLY1_SetHigh();
RLY3_SetHigh();
}
else
{
RLY1_SetLow();
RLY3_SetLow();
}
}
}
}
I am working on a project where I have to establish a communication between my microprocessor and a Bluetooth device. I established a communication, but no matter what I send, I only get 0 when I print it. Thanks for the help !
#include <asf.h>
#include <string.h>
#include <stdio.h>
#include "usart.h"
/**
* Configure serial console.
*/
static void configure_console(void)
{
const usart_serial_options_t uart_serial_options = {
.baudrate = CONF_UART_BAUDRATE,
#ifdef CONF_UART_CHAR_LENGTH
.charlength = CONF_UART_CHAR_LENGTH,
#endif
.paritytype = CONF_UART_PARITY,
#ifdef CONF_UART_STOP_BITS
.stopbits = CONF_UART_STOP_BITS,
#endif
};
/* Configure console. */
stdio_serial_init(CONF_UART, &uart_serial_options);
}
volatile uint32_t ul_ms_ticks = 0;
static void mdelay(uint32_t ul_dly_ticks)
{
uint32_t ul_cur_ticks;
ul_cur_ticks = ul_ms_ticks;
while ((ul_ms_ticks - ul_cur_ticks) < ul_dly_ticks) {
}
}
void SysTick_Handler(void)
{
ul_ms_ticks++;
}
#define USART_SERIAL USART1
#define USART_SERIAL_ID ID_USART1 //USART1 for sam4l
#define USART_SERIAL_BAUDRATE 9600
#define USART_SERIAL_CHAR_LENGTH US_MR_CHRL_8_BIT
#define USART_SERIAL_PARITY US_MR_PAR_NO
#define USART_SERIAL_STOP_BIT US_MR_NBSTOP_1
int main(void)
{
/* Initialize the SAM system */
sysclk_init();
board_init();
/* Initialize the console uart */
configure_console();
uint32_t a= 1234;
int bit_rx = 6;
if (SysTick_Config(sysclk_get_cpu_hz() / 1000)) {
while (1) { /* Capture error */
}
}
const sam_usart_opt_t usart_console_settings = {
USART_SERIAL_BAUDRATE,
USART_SERIAL_CHAR_LENGTH,
USART_SERIAL_PARITY,
USART_SERIAL_STOP_BIT,
US_MR_CHMODE_NORMAL
};
#if SAM4L
sysclk_enable_peripheral_clock(USART_SERIAL);
#else
sysclk_enable_peripheral_clock(USART_SERIAL_ID);
#endif
usart_enable_tx(USART_SERIAL);
usart_enable_rx(USART_SERIAL);
while (1) {
printf("%d \n", bit_rx );
usart_getchar(USART_SERIAL, &a);
bit_rx = a;
printf("%d \n",bit_rx );
if (a != 0) {
ioport_set_pin_level(LED_0_PIN, LED_0_ACTIVE);
} else {
ioport_set_pin_level(LED_0_PIN, !LED_0_ACTIVE);
}
}
}
Perhaps you shouldn't call usart_getchar() until usart_is_rx_ready() returns 1.
Thanks everyone !
I tried to cast the received variable first : bit_rx = (int) a;
And then I found that I needed this to make it work :
static usart_serial_options_t usart_options = {
.baudrate = USART_SERIAL_BAUDRATE,
.charlength = USART_SERIAL_CHAR_LENGTH,
.paritytype = USART_SERIAL_PARITY,
.stopbits = USART_SERIAL_STOP_BIT
};
usart_serial_init(USART_SERIAL, &usart_options);
And I disabled the stop bit, because my bluetooth didn't have one :
#define USART_SERIAL_STOP_BIT false
Now it works like a charm. Thanks all for your help !