I am trying to have a raspberry pi controll the status of GPIO pins which are connected to a relay board for a christmas light display, the issue is that the pins are stuck in the on position and i get the error "Unhandled Event 10" pretty often
Here is the code that controlls the pins based on midi playback.
#include <alsa/asoundlib.h>
#include <wiringPi.h>
#include <limits.h>
#include <unistd.h>
#include <math.h>
static snd_seq_t *seq_handle;
static int in_port;
//16 melody channels, using wiringPi numbering
int pinMapping[] = {
0, //0
1, //1
2, //2
3, //3
4, //4
5, //5
6, //6
21, //7
22, //8
26, //9
23, //10
24, //11
27, //12
25, //13
28, //14
29, //15
};
#define TOTAL_PINS sizeof(pinMapping) / sizeof(int)
#define THRUPORTCLIENT 14
#define THRUPORTPORT 0
void midi_open(void)
{
snd_seq_open(&seq_handle, "default", SND_SEQ_OPEN_INPUT, 0);
snd_seq_set_client_name(seq_handle, "LightOrgan");
in_port = snd_seq_create_simple_port(seq_handle, "listen:in",
SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE,
SND_SEQ_PORT_TYPE_APPLICATION);
if( snd_seq_connect_from(seq_handle, in_port, THRUPORTCLIENT, THRUPORTPORT) == -1) {
perror("Can't connect to thru port");
exit(-1);
}
}
snd_seq_event_t *midi_read(void)
{
snd_seq_event_t *ev = NULL;
snd_seq_event_input(seq_handle, &ev);
return ev;
}
//Currently playing note, by pin
int pinNotes[TOTAL_PINS];
//Currently playing channel, by pin
int pinChannels[TOTAL_PINS];
//Enabled channels
int playChannels[16];
void clearPinNotes() {
int i;
for(i=0; i< TOTAL_PINS; i++) {
pinNotes[i] = -1;
}
}
void myDigitalWrite(int pinIdx, int val) {
val ? printf("%i (%i) ON\n", pinIdx, pinMapping[pinIdx]) : printf("%i (%i) OFF\n", pinIdx, pinMapping[pinIdx]);
digitalWrite( pinMapping[pinIdx], val );
}
void clearPinChannels() {
int i;
for(i=0; i< TOTAL_PINS; i++) {
pinChannels[i] = INT_MAX;
}
}
void clearPinsState() {
clearPinNotes();
clearPinChannels();
}
void pinsOn() {
int i;
for(i=0; i< TOTAL_PINS; i++) {
myDigitalWrite(i, 1);
}
}
void pinsOff() {
int i;
for(i=0; i< TOTAL_PINS; i++) {
myDigitalWrite(i, 1);
}
}
void setChannelInstrument(int channel, int instr) {
printf("setting channel %i to instrument %i\n", channel, instr);
playChannels[channel] = instr;
}
int isPercussion(int instrVal) {
return instrVal >= 8 && instrVal <= 15;
}
int isPercussionChannel(int channel) {
int instr = playChannels[channel];
return isPercussion(instr);
}
int isBase(int instrVal) {
return instrVal >= 32 && instrVal <= 39;
}
int isSynth(int instrVal) {
return instrVal >= 88 && instrVal <= 103;
}
int choosePinIdx(int note, int channel) {
//Return the note modulated by the number of melody pins
int val = note % (TOTAL_PINS * 2);
return val / 2;
}
void midi_process(snd_seq_event_t *ev)
{
//If this event is a PGMCHANGE type, it's a request to map a channel to an instrument
if( ev->type == SND_SEQ_EVENT_PGMCHANGE ) {
//printf("PGMCHANGE: channel %2d, %5d, %5d\n", ev->data.control.channel, ev->data.control.param, ev->data.control.value);
//Clear pins state, this is probably the beginning of a new song
clearPinsState();
setChannelInstrument(ev->data.control.channel, ev->data.control.value);
}
//Note on/off event
else if ( ((ev->type == SND_SEQ_EVENT_NOTEON)||(ev->type == SND_SEQ_EVENT_NOTEOFF)) ) {
//choose the output pin based on the pitch of the note
int pinIdx = choosePinIdx(ev->data.note.note, ev->data.note.channel);
if(!isPercussionChannel(ev->data.note.channel) ) {
int isOn = 1;
//Note velocity == 0 means the same thing as a NOTEOFF type event
if( ev->data.note.velocity == 0 || ev->type == SND_SEQ_EVENT_NOTEOFF) {
isOn = 0;
}
//If pin is set to be turned on
if( isOn ) {
//If pin is currently available to play a note, or if currently playing channel can be overriden due to higher priority channel
if( pinNotes[pinIdx] == -1 || pinChannels[pinIdx] > ev->data.note.channel ) {
if( (pinChannels[pinIdx] > ev->data.note.channel ) && pinNotes[pinIdx] != -1) {
//printf("OVERRIDING CHANNEL %i for %i\n", pinChannels[pinIdx], ev->data.note.channel);
}
//Write to the pin, save the note to pinNotes
//printf("Pin %i - %s %i %i \n", pinIdx, isOn ? "on" : "off", ev->data.note.note, ev->data.note.channel);
myDigitalWrite(pinIdx, 1);
pinNotes[pinIdx] = ev->data.note.note;
pinChannels[pinIdx] = ev->data.note.channel;
}
}
//Pin is to be turned off
else {
//If this is the note that turned the pin on..
if( pinNotes[pinIdx] == ev->data.note.note && pinChannels[pinIdx] == ev->data.note.channel ) {
//Write to the pin, indicate that pin is available
//printf("Pin %i - %s %i %i \n", pinIdx, isOn ? "on" : "off", ev->data.note.note, ev->data.note.channel);
myDigitalWrite(pinIdx, 0);
pinNotes[pinIdx] = -1;
pinChannels[pinIdx] = INT_MAX;
}
}
}
}
else {
printf("Unhandled event %2d\n", ev->type);
}
snd_seq_free_event(ev);
}
int main()
{
//Setup wiringPi
if( wiringPiSetup() == -1) {
exit(1);
}
//Setup all the pins to use OUTPUT mode
int i=0;
for(i=0; i< TOTAL_PINS; i++) {
pinMode( pinMapping[i], OUTPUT);
}
clearPinsState();
//Open a midi port, connect to thru port also
midi_open();
//Process events forever
while (1) {
midi_process(midi_read());
}
return -1;
}
I was hoping that the pins would trun on and off with the music but the pins appear to be stuck on.
Related
I am implementing simon says as a small weekly project for school. Using arduino Uno, I'm making 10 levels each level has an extra pattern inside. example: level 1: [1], level 2: [1,2], etc... I have three buttons on my shield. the interrupts work and everything is gucci. My problem here is in this snippet
bool readInput(uint8_t pattern[], uint8_t length)
{
sei();
uint8_t current = 0;
while (current < length)
{
btnPushed = false;
while (!btnPushed)
{
#ifdef DEBUG
_delay_ms(1);
#endif
}
printf("here");
cli();
_delay_ms(200);
if (currentPushed == pattern[current])
{
printf("correct, you pushed %d\n", currentPushed);
}
else
{
printf("incorrect, lets try again\n");
return false;
}
}
btnPushed = false;
return true;
}
so basically I set my buttonPushed to false, and start listening for interrupts, once its true after clicking, I expect to exit the loop and check the input, however my interrupt is correct and I get visual feedback with a light that lights up once i push a button.
this is my ISR
ISR(PCINT1_vect)
{
uint8_t buttonCurr = currentButton();
if (buttonCurr != -1)
{
if (!btn1Pushed && buttonCurr == 0)
{
btn1Pushed = true;
}
currentPushed = buttonCurr;
blinkLed(currentPushed, 1);
btnPushed = true;
}
}
my current button returns 0-2 for buttons that are clicked, and -1 if nothing was clicked.
this is the rest of my code, which is working pretty much
int currentPushed = -1;
bool won;
bool btnPushed = false;
bool btn1Pushed = false;
ISR(PCINT1_vect)
{
uint8_t buttonCurr = currentButton();
if (buttonCurr != -1)
{
if (!btn1Pushed && buttonCurr == 0)
{
btn1Pushed = true;
}
currentPushed = buttonCurr;
blinkLed(currentPushed, 1);
btnPushed = true;
}
}
int main(void)
{
enableAllButtons();
enableAllLeds();
lightDownAllLeds();
prepareButtonsForInterrupt();
initUSART();
init();
play();
if (won)
{
printf("Simon says you win");
}
else
{
printf("simon says do better");
}
return 0;
}
void init(void)
{
printf("LETS PLAY SIMON SAYS\nPress button 1 to start!\n");
int seed = 0;
while (!btn1Pushed)
{
blinkLed(3, 4);
seed++;
}
srand(seed);
printf("Get ready!\n");
btnPushed = false;
cli();
}
void createRandomPattern(uint8_t array[], uint8_t length)
{
for (int i = 0; i < length; i++)
{
array[i] = rand() % 3;
}
}
void play(uint8_t pattern[])
{
uint8_t fullPattern[MAX_PATTERN_LENGTH];
createRandomPattern(fullPattern, MAX_PATTERN_LENGTH);
for (int i = 1; i <= MAX_PATTERN_LENGTH; i++)
{
printf("========LEVEL %d===========\n", i);
playPuzzle(fullPattern, i);
#ifdef DEBUG
printPuzzle(fullPattern, i);
#endif
readInput(fullPattern, i) ?: i--;
}
}
bool readInput(uint8_t pattern[], uint8_t length)
{
sei();
uint8_t current = 0;
while (current < length)
{
btnPushed = false;
while (!btnPushed)
{
}
cli();
if (currentPushed == pattern[current])
{
printf("correct, you pushed %d\n", currentPushed);
}
else
{
printf("incorrect, lets try again\n");
return false;
}
current++;
}
btnPushed = false;
return true;
}
void printPuzzle(uint8_t pattern[], uint8_t length)
{
printf("[ ");
for (int i = 0; i < length; i++)
{
printf("%d ", pattern[i]);
}
printf("]\n");
}
void playPuzzle(uint8_t pattern[], uint8_t length)
{
for (int i = 0; i < length; i++)
{
lightUpOneLed(pattern[i]);
_delay_ms(500);
lightDownOneLed(pattern[i]);
}
}
btnPushed is defined as bool btnPushed = false;.
So when you write:
while (!btnPushed)
{
#ifdef DEBUG
_delay_ms(1);
#endif
}
Nothing in the loop will change btnPushed so there is no point for the compiler to ever check btnPushed again. So what the compiler sees is this:
if (!btnPushed)
while(true)
{
#ifdef DEBUG
_delay_ms(1);
#endif
}
You have to tell the compiler that the value of btnPushed will change unexpectantly when the interrupt fires by using:
volatile bool btnPushed = false;
Beginner here learning to code linux kernel modules.
I want to write a kernel module for my RPi 4 in C language.
I want to use interrupts to light on a LED when I push a button. As long as the button is pushed, the LED is on and when I release it, the LED is supposed to turn off.
I use the function request_irq() so that my function handling the interrupt is called on the rising edge and on the falling edge of my button by indicating "IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING". I formerly used the function gpio_to_irq(BUTTON_PIN).
request_irq(button_irq, button_ih, IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, "button_irq", NULL);
The function is called when I push the button but not when I release it. It is coded as seen below :
static irqreturn_t button_ih(int irq, void *data)
{
int state;
state = gpio_get_value(BUTTON_PIN);
printk(KERN_INFO "Actual state of the button : %d", state);
//Debounce condition
if(jiffies - last_interrupt_time > msecs_to_jiffies(200))
{
if(state)
{
gpio_set_value(LED_PIN, 1);
}else
{
gpio_set_value(LED_PIN, 0);
}
}
last_interrupt_time = jiffies;
return IRQ_HANDLED;
}
I check whether the value of the button is 1 (pressed) or 0 (released) and want to turn on/off the LED accordingly.
When I try different GPIOs, sometimes, the value of my button is 1 even though I haven't pushed it yet(by printing a message from the init function) and sometimes it's 0. So I don't understand if I'm doing something wrong. I don't think the GPIOs are already used cause I can request them with the function gpio_request_one().
So when the button already has the value 1, the LED turn on but won't switch off when I release the button.
And when the value is already 0, the button value doesn't change to become 1 when I enter the interrupt handler function (and the LED obviously doesn't turn on).
Could you tell me what's wrong with it please?
Form simple C program, not kernel module
This minimal to see how I'm doing.
main.h
typedef struct s_Data {
int exitFlag;
struct {int value; int bcmPin;} In01; //<--- Stock actual GPIO Value
struct {int value; int bcmPin;} In02;
....
struct {int value; int bcmPin;} Out01;
struct {int value; int bcmPin;} Out02;
....
} t_Data;
#define NO_Click 0
#define SimpleClick 1
#define LongClick 2
#define MultiClick 3
typedef struct s_Button {
pthread_t button_thread;
int *exitFlag;
int * input; //Button
int * output;
int old_state;
int new_state;
struct timeval t0;
struct timeval t1;
struct timeval pressedTime;
struct timeval releasedTime;
int clickType;
//float debounce = 0.020; // ms debounce period to prevent flickering when pressing or releasing the button
// debounce not necessary while thread have time to take a coffee
float DCgap; // max ms between clicks for a double click event
float holdTime; // ms hold period: how long to wait for press+hold event
} t_Button;
main.c
#include "main.h"
#include <wiringPi.h>
#include <pthread.h>
// initial state
static t_Data data = {
.exitFlag = 0,
.In01.value = 0, .In01.bcmPin = 4,
.In02.value = 0, .In02.bcmPin = 17,
.Out01.value = 0, .Out01.bcmPin = 4,
.Out02.value = 0, .Out02.bcmPin = 17,
}
static _Data data_prev;
static void *inputRead_threadFn(void *p_Data) {
pinMode(Data.In01.bcmPin, INPUT);
pullUpDnControl(Data.In01.bcmPin, PUD_UP);
pinMode(Data.In02.bcmPin, INPUT);
pullUpDnControl(Data.In02.bcmPin, PUD_UP);
while (!Data.exitFlag) {
Data.In01.value = !digitalRead(Data.In01.bcmPin);
Data.In02.value = !digitalRead(Data.In02.bcmPin);
if (Data.In01.value != Data_old.In01.value) {
Data_old.In01.value = Data.In01.value;
}
if (Data.In02.value != Data_old.In02.value) {
Data_old.In02.value = Data.In02.value;
}
usleep(50)
}
}
static void *outputWrite_threadFn(void *p_Data) {
pinMode(Data.In01.bcmPin, OUTPUT);
pinMode(Data.In02.bcmPin, OUTPUT);
while (!Data.exitFlag) {
digitalWrite(Data.Out01.bcmPin, !Data.Out01.value);
digitalWrite(Data.Out02.bcmPin, !Data.Out02.value);
}
usleep(50)
}
static void *button_threadFn(void *p_Data) {
t_Button *button = (t_Button *)p_data;
LOG("Button[%s] thread initialized\r\n", button->name);
button->old_state = 0;
button->new_state = 0;
button->clickType = NO_Click;
int clickCount = 0;
while(*(button->exitFlag) == 0) {
button->new_state = *(button->input) || *(button->web_input); //*((int *)
if( button->old_state == 0 && button->new_state == 1 ) {
//printf("front montant\r\n"); fflush(stdout);
// *****************************
// traitement sur front montant
// rising edge
// *****************************
button->old_state = 1;
gettimeofday(&button->pressedTime, 0);
//Button pressed
} else if( (button->old_state == 1) && (button->new_state == 0) ) {
//printf("front descendant\r\n"); fflush(stdout);
// *****************************
// traitement sur front descendant
// falling edge
// *****************************
button->old_state = 0;
gettimeofday(&button->releasedTime, 0);
if (my_timedifference_msec(button->releasedTime, button->pressedTime ) < button->DCgap) {
clickCount++;
button->clickType = MultiClick;
}
//Button released
} else if( (button->old_state == 0) && (button->new_state == 0) ) {
// printf("front bas\r\n"); fflush(stdout);
// *****************************
// pas de changement d'état : front bas
// no state change : edge down
// *****************************
gettimeofday(&button->t0, 0);
*(button->output) = 0; //<--- here in your case
//Attendre DC_Gap pour connaitre le nombre de click
// Wait for DC_Gap to know click count
if (my_timedifference_msec(button->t0, button->releasedTime) > button->DCgap) {
if (clickCount == 1) {
LOG("SimpleClick");
//Simple Click
} else if ( clickCount > 1 ) {
//Multiclicks
}
button->clickType = NO_Click;
clickCount = 0;
}
} else if( (button->old_state == 1) && (button->new_state == 1) ) {
// printf("front haut\r\n"); fflush(stdout);
// *****************************
// pas de changement d'état : front haut
// no state change : edge up
// *****************************
gettimeofday(&button->t1, 0);
*(button->output) = 1; //<--- here in your case
//long click
if (my_timedifference_msec(button->t1, button->pressedTime) >= button->holdTime) {
LOG("LongClick");
button->clickType = LongClick;
//do what you want while not released
usleep(30*1000);
}
}
usleep(100);
}
printf("Light Loop::exiting...\r\n"); fflush(stdout);
}
int main(int argc, char** argv) {
wiringPiSetup();
wiringPiSetupGpio();
data_prev = data;
//start input thread
//start output thread
int DCGap = 250; //ms
int HoldTime = 600;
t_Button Buttons[] = {
{ //WC
.exitFlag = &Data.exitFlag,
.DCgap = DCGap,
.holdTime = HoldTime,
.input = &Data.In01.value,
.output = &Data.Out01.value,
},
{ //chambre
.exitFlag = &Data.exitFlag,
.DCgap = DCGap,
.holdTime = HoldTime,
.input = &Data.In02.value,
.output= &Data.Out02.value,
}
}
//start buttons threads
for (i = 0; i < (sizeof(Buttons) / sizeof(t_Button)) ; i++) {
ret = pthread_create (&Buttons[i].button_threadFn, NULL, fn_Button, &Buttons[i]);
if (ret) {
fprintf (stderr, "%s", strerror (ret));
}
}
//threads join
return (EXIT_SUCCESS);
}
Ok so thanks for all the comments/answers.
It was indeed because I wasn't using any pull-up or pull-down resistor properly.
So I changed my circuit as following:
Circuit
So currently I am trying to write a code where the client sends a code and a number to the server where the server will use that code to figure out which conversion it needs to make for the number. As of now I have the whole program written up but only for one client. How do I make it to where it accepts up to 10 clients?
/* convserver2.cc
*
* Created by: Aaron Eiser
* 2/26/2020
*/
#include "socket.h"
#include "fdset.h"
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <ctype.h>
#include <stdlib.h>
#include <signal.h>
void ctrlCHandler(int signal);
float conversion(unsigned char code, float value);
bool isValid(unsigned char code);
const unsigned char CONV_CODES[] = {0, 11, 12, 13, 14, 15, 16, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32};
const int LENGTH = 19;
const int MAX_CLIENTS = 10;
int clientSock[10];
int master_socket, new_socket, activity, valread, sd, max_sd;
ServerSocket theServer;
Socket *theClient = NULL;
// struct for packet
struct Packet{
float value;
char gap[3];
unsigned char code;
};
int main(int argc, char *argv[])
{
fd_sett readfds;
int port = atoi(argv[1]);
for(i = 0; i < MAX_CLIENTS; i++){
clientSock[i] = 0;
}
/* Bind the server listening socket to the given port. */
theServer.bind(port);
printf("Server bound to port #%d\n", port);
Packet data;
bool flags = true;
while(flags){
signal(SIGINT, ctrlCHandler);
theClient = theServer.accept();
printf("Connection made\n");
int size = theClient -> recv(&data, sizeof(Packet));
printf("Packet receieved\n");
while(size > 0 and data.code != 0){
Packet newData;
if(!isValid(data.code)){
fprintf(stderr, "Invalid code\n");
newData.code = 0;
newData.value = conversion(data.code, data.value);
theClient -> send(&newData, sizeof(Packet));
printf("Packet sent\n");
size = theClient -> recv(&data, sizeof(Packet));
printf("Packet received\n");
}
else{
newData.code = data.code;
newData.value = conversion(data.code, data.value);
theClient -> send(&newData, sizeof(Packet));
printf("%d %f \n", newData.code, newData.value);
printf("Packet sent\n");
size = theClient -> recv(&data, sizeof(Packet));
printf("Packet received\n");
}
}
theClient -> close();
delete theClient;
}
}
float conversion(unsigned char code, float value){
float newValue;
// Conv code 11
if(code == 11){
newValue = (value - 32) * (5.0 / 9.0);
printf("something\n");
}
// Conv code 12
else if(code == 12){
newValue = (value - 32) * (5.0 / 9.0) + 273.15;
}
// Conv code 13
else if(code == 13){
newValue = (value * (9.0 / 5.0)) + 32;
}
// Conv code 14
else if(code == 14){
newValue = value + 273.15;
}
// Conv code 15
else if(code == 15){
newValue = (value - 273.15) * (9.0 / 5.0) + 32;
}
// Conv code 16
else if(code == 16){
newValue = value - 273.15;
}
// Conv code 21
else if(code == 21){
newValue = value / 12.0;
}
// Conv code 22
else if(code == 22){
newValue = value / 36.0;
}
// Conv code 23
else if(code == 23){
newValue = value / 63360.0;
}
// Conv code 24
else if(code == 24){
newValue = value * 12.0;
}
// Conv code 25
else if(code == 25){
newValue = value / 3.0;
}
// Conv code 26
else if(code == 26){
newValue = value / 5280.0;
}
// Conv code 27
else if(code == 27){
newValue = value * 36.0;
}
// Conv code 28
else if(code == 28){
newValue = value * 3.0;
}
// Conv code 29
else if(code == 29){
newValue = value / 1760.0;
}
// Conv code 30
else if(code == 30){
newValue = value * 63360.0;
}
// Conv code 31
else if(code == 31){
newValue = value * 5280.0;
}
// Conv code 32
else if(code == 32){
newValue = value * 1760.0;
}
return newValue;
}
// Decides whether code is valid
bool isValid(unsigned char code){
for(int i = 0; i < LENGTH; i++){
if(code == CONV_CODES[i]){
return true;
}
}
return false;
}
// Catches Ctrl C
void ctrlCHandler(int signal){
if(theClient != NULL){
theClient -> close();
}
theServer.close();
exit(0);
}
I have some problems with transmiting and receiving using usart. First informations that I want transmit are writen to circle buffer. Then indormations are send but some informations are lost.
Variables
enum {
BUF_SIZE = 100
};
char BUF_T[BUF_SIZE], BUF_R[BUF_SIZE];
uint8_t R_EMPTY = 0, R_BUSY = 0, T_EMPTY = 0, T_BUSY = 0;
Transmiting
void Send(void) {
uint8_t index = T_EMPTY;
for (int i = 0; i < 10; i++) {
BUF_T[index] = i+'0';
index++;
if (index >= BUF_SIZE) {
index = 0;
}
}
__disable_irq();
if (T_BUSY == T_EMPTY
&& __HAL_UART_GET_FLAG(&huart2,UART_FLAG_TXE) == SET) {
T_EMPTY = index;
T_BUSY++;
uint8_t tmp = BUF_T[T_BUSY];
if (T_BUSY >= BUF_SIZE) {
T_BUSY = 0;
}
HAL_UART_Transmit_IT(&huart2, (uint8_t*) &tmp, 1);
} else {
T_EMPTY = index;
}
__enable_irq();
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) {
//if (huart->Instance == USART2) {
if (T_BUSY != T_EMPTY) {
__disable_irq();
T_BUSY++;
uint8_t tmp = BUF_T[T_BUSY];
if (T_BUSY >= BUF_SIZE) {
T_BUSY = 0;
}
HAL_UART_Transmit_IT(&huart2, (uint8_t*) &tmp, 1);
__enable_irq();
}else {
Send();
}
//}
}
Screen from hterm
On picture can be seen that from time to time one char is lost. I don't understant why?
Regarding receiving can somebody tell mi if it OK?
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
//if (huart->Instance == USART2) {
uint8_t tmp = BUF_R[R_EMPTY];
R_EMPTY++;
if (R_EMPTY >= BUF_SIZE) {
R_EMPTY = 0;
}
HAL_UART_Receive_IT(&huart2, (uint8_t*) &tmp, 1);
//}
}
I am working with PIC32MX350F128L.The uart1 which is PIN 22 for (TX) and pin 20 for (RX). I am able to send a character but not able to receive any character.Below is the code attached.
Please do have a look at code and suggest some changes ?
void init_uart();
void UART1PutChar(unsigned char Ch);
char SerialReceive_1();
void main()
{
char res;
OSCCON=0x00002200; //ask settings clock
ANSELBbits.ANSB3 = 0;
ANSELBbits.ANSB5 = 0;
TRISCbits.TRISC3 = 0;
TRISCbits.TRISC4 = 0;
PORTCbits.RC3 = 1;
PORTCbits.RC4 = 1;
TRISBbits.TRISB3 = 0; //in controler tx
TRISBbits.TRISB5 = 1; // in controller RX
U1RXR=0x08;
RPB3R=0x03;
init_uart() ;
UART1PutChar('T');
while(1)
{
res = SerialReceive_1();
UART1PutChar(res);
}
}
void init_uart()
{
U1MODEbits.ON =1;
U1BRG = 25;
U1STA=0x1400;
IFS1bits.U1RXIF = 0;
IFS1bits.U1TXIF = 0 ;
return;
}
void UART1PutChar(unsigned char Ch)
{
while(U1STAbits.UTXBF == 1);
U1TXREG=Ch;
return ;
}
char SerialReceive_1()
{
char buf1;
while(!U1STAbits.URXDA);
buf1 = U1RXREG;
return buf1;
}
Regards