Motor runs despite not calling it - c

currently doing a project with a board with several routines:
#include <xc.h>
#include "config.h"
#define TRUE 1
#define FALSE 0
#define SW1 PORTBbits.RB4
#define SW2 PORTBbits.RB5
#define PB3 PORTCbits.RC3 //for testing
#define PB2 PORTDbits.RD1
#define PB7 PORTCbits.RC7
#define PB8 PORTCbits.RC6
#define MOTOR PORTDbits.RD0
void initSysPins(void);
void ReadLight(void);
void ReadTemperature(void);
void ReadSoilMoisture(void);
void MessageDoze(void);
void MessageSleep(void);
void lcdClear(void);
void initLCD(void);
void initSysInt(void);
void initSysTimer(void);
void pwrMgmt(void);
void initPWM(void);
unsigned char detPB3(void); //for testing
unsigned char detPB2(void);
unsigned char detPB7(void);
unsigned char detPB8(void);
void main(void) {
initSysInt();
initSysPins();
initSysTimer();
CPUDOZE = 0;
while(TRUE){
if(SW1 == 1 && SW2 == 0){
ReadTemperature();
}
else if (SW1 == 0 && SW2 == 1){
ReadLight();
}
else if (SW1 == 1 && SW2 == 1){
ReadSoilMoisture();
}
else {
lcdClear();
}
pwrMgmt();
//pwrMgmt1();
}
}
void pwrMgmt(void){
if(PB2 == 0){
lcdClear();
ROI = 1;
MessageSleep();
SLEEP();
__delay_ms(500);
}
else if(PB7 == 0){
lcdClear();
MessageDoze();
DOZEN= 1;
ROI = 1;
}
}
unsigned char detPB3(void)
{
unsigned char detect = 0; //initial status, sw1 not pressed
if(PB3 == 0) //if sw1 is pressed
{
__delay_ms(20); // 20ms delay to skip contact bounce
if(PB3 == 0) //if still pressed
{
detect = 1; //confirm sw1 is pressed n set detect
while(PB3 == 0);//wait for sw1 to be released
}
}
return detect;
}
unsigned char detPB2(void)
{
unsigned char detect = 0; //initial status, sw1 not pressed
if(PB2 == 0) //if sw1 is pressed
{
__delay_ms(20); // 20ms delay to skip contact bounce
if(PB2 == 0) //if still pressed
{
detect = 1; //confirm sw1 is pressed n set detect
while(PB2 == 0);//wait for sw1 to be released
}
}
return detect;
}
unsigned char detPB7(void)
{
unsigned char detect = 0; //initial status, sw1 not pressed
if(PB7 == 0) //if sw1 is pressed
{
__delay_ms(20); // 20ms delay to skip contact bounce
if(PB7 == 0) //if still pressed
{
detect = 1; //confirm sw1 is pressed n set detect
while(PB7 == 0);//wait for sw1 to be released
}
}
return detect;
}
unsigned char detPB8(void)
{
unsigned char detect = 0; //initial status, sw1 not pressed
if(PB8 == 0) //if sw1 is pressed
{
__delay_ms(20); // 20ms delay to skip contact bounce
if(PB8 == 0) //if still pressed
{
detect = 1; //confirm sw1 is pressed n set detect
while(PB8 == 0);//wait for sw1 to be released
}
}
return detect;
}
The only problem is that my DC motor still runs on its own despite only being called inside my soil moisture function. the other functions just use LCD and LED and work fine. I've tried working around this by putting MOTOR = 0; inside my main but that doesn't do anything.
EDIT: Works on its own before integration with the rest of my codes.
ReadSoilMoisture():
#include <xc.h>
#include "config.h"
#define lcd_Clear 0b00000001
#define LCD_DATA PORTD
#define LCD_RS PORTEbits.RE1 //RS signal for LCD
#define LCD_E PORTEbits.RE0 // E signal for LCD
#define MOTOR PORTDbits.RD0
void initSysPins(void);
void lcdWriteData(char);
void lcdWriteCtrl(char);
void lcdWriteMessage(char message[], unsigned char row);
void lcdSetPos(unsigned char row, unsigned char col);
void initPWM(void);
//void Message(void);
void Message1(void);
void Message2(void);
void Message(void);
void initLCD(void);
void initADC(void);
unsigned int readPot1(void);
void ReadSoilMoisture(void) {
unsigned int raw;
unsigned char pwmCycle = 0;
// float fresult;
// char res[20];
initLCD();
initSysPins();
initPWM();
initADC();
PWM6CONbits.EN = 1;
// raw = readPot1();
int i = 0;
for(i = 0; i<=40; i++) {
raw = readPot1();
if (raw == 0 && raw <= 10)
{
PORTA = 0x00;
}
else if (raw == 11 && raw <=100)
{
PORTA = 0b00001001; //RED
MOTOR = 1;
pwmCycle = 4000;
__delay_ms(5000);
PWM6DCH = pwmCycle >> 2;
PWM6DCL = (pwmCycle % 4)<< 6;
Message();
__delay_ms(3000);
lcdWriteCtrl(0b00000001); // Clear display & home position
}
else if (raw == 101 && raw <= 1000)
{
PORTA = 0b00010010; //YELLOW
MOTOR = 1;
pwmCycle = 1500;
__delay_ms(3000);
PWM6DCH = pwmCycle >> 2;
PWM6DCL = (pwmCycle % 4)<< 6;
Message1();
__delay_ms(3000);
lcdWriteCtrl(0b00000001); // Clear display & home position
}
else if (raw == 1001 && raw <= 1023)
{
PORTA = 0b00100100; //GREEN
MOTOR = 1;
pwmCycle = 900;
__delay_ms(800);
PWM6DCH = pwmCycle >> 2;
PWM6DCL = (pwmCycle % 4)<< 6;
Message2();
__delay_ms(3000);
lcdWriteCtrl(0b00000001); // Clear display & home position
}
}
}
void Message(void){
char message[] = "Soil Moisture";
char message1[] = "Is 55%";
unsigned char i;
lcdSetPos(1,1); // Set DD RAM pos to row 1, col 3
for(i = 0; message[i] != 0; i++) {
lcdWriteData(message[i]); // Display one character
}
lcdSetPos(2,1); // Set DD RAM pos to row 2, col 1
for(i = 0; message1[i] != 0; i++) {
lcdWriteData(message1[i]); // Display one character
}
}
void Message1(void){
char message[] = "Soil Moisture";
char message1[] = "Is 75%";
unsigned char i;
lcdSetPos(1,1); // Set DD RAM pos to row 1, col 3
for(i = 0; message[i] != 0; i++) {
lcdWriteData(message[i]); // Display one character
}
lcdSetPos(2,1); // Set DD RAM pos to row 2, col 1
for(i = 0; message1[i] != 0; i++) {
lcdWriteData(message1[i]); // Display one character
}
}
void Message2(void){
char message[] = "Soil Moisture";
char message1[] = "Is 95%";
unsigned char i;
lcdSetPos(1,1); // Set DD RAM pos to row 1, col 3
for(i = 0; message[i] != 0; i++) {
lcdWriteData(message[i]); // Display one character
}
lcdSetPos(2,1); // Set DD RAM pos to row 2, col 1
for(i = 0; message1[i] != 0; i++) {
lcdWriteData(message1[i]); // Display one character
}
}
void initPWM(void)
{
CCPTMRS1 = PORTDbits.RD0;
PWM6CON = 0b00000000;
T2PR = 249;
PWM6DCH = 0b01111101;
PWM6DCL = 0b00000000;
T2CON = 0b11110000;
T2CLKCON = 0b00000001;
RA2PPS = 0x0E;
}
Any guess as to what I can do?

Related

Extract the data from array_buffer and stored in the variable

I am receiving data continuously using UART and stored in the buffer in the following form::
$GNRMC,114811.00,A,3558.94849,N,14008.90158,E,0.076,,141115,,,A*69
$GNVTG,,T,,M,0.076,N,0.141,K,A*38
$GNGSA,A,3,,,,,,,,,,,,,3.03,1.18,2.80*1E
$GPGSV,3,2,09,20,04,163,,22,49,280,29,24,45,048,33,25,53,183,28*75
$GNGLL,3558.94849,N,14008.90158,E,114811.00,A,A*70*
I want to extract only the $GNRMCdata and store it on some variable year, mon, day, hour, min, sec so that I can display that in 16*2LCD. The value right after $GNRMC 114811 is the time. But I am not getting the data on those variables. Where I am wrong?
void gps_getinfo(unsigned char rx2_id_buff[]);
void uart2_rx_int(void);
#define MAX_LINEBUF 70
char gps_linebuf[MAX_LINEBUF];
unsigned char time_flag;
unsigned char rx2_id_buff[70];
unsigned char rx2_id_pointer;
unsigned char year[4]={0},mon[4]={0},day[4]={0};
unsigned char hour[4]={0},min[4]={0},sec[4]={0};
#pragma interrupt uart2_rx_int (vect=0x16)
void uart2_rx_int(void)
{
unsigned char error;
error = SSR11 & 0x07;
rx2_id = SDR11;
if (error == 0) {
if(rx2_id == '$' ) rx2_id_pointer = 0;
rx2_id_buff[rx2_id_pointer] = rx2_id; //storing data in the buffer received form UART
gps_getinfo(rx2_id_buff[rx2_id_pointer]);
}
else
rx2_id_buff[rx2_id_pointer] = err | 0xF0;
rx2_id_pointer++;
if (rx2_id_pointer > 69) rx2_id_pointer = 0;
}
void gps_getinfo(unsigned char rx2_id_buff[]) {
short i,j,len;
char *ptr,*ptrTop;
unsigned char j1,j2;
unsigned char comma[20]={0};
if (rx2_id_buff != '\n') {
gps_linebuf[idx++] = rx2_id_buff;
if(idx >= MAX_LINEBUF)idx=0;
}
else {
gps_linebuf[idx++] = '\n';
gps_linebuf[idx] = '\0';
i = MAX_LINEBUF;
j = idx - 1;
while(1) {
if(gps_linebuf[j] == '$') break;
j--;
if(j < 0) j = MAX_LINEBUF - 1;
if(i-- == 0) {
gps_linebuf[0] = 0; idx=0;
return;
}
}
gps_linebuf[idx] = 0; idx=0;
ptrTop = (char *)&gps_linebuf[j];
ptr = (char *)&gps_linebuf[j];
//$GNRMC,114811.00,A,3558.94849,N,14008.90158,E,0.076,,141115,,,A*69
if(strstr(ptrTop,"GNRMC")!=0) {
len=strlen(ptr);
j=0;
for(i=0; i<len; ++i,ptr++) {
if(*ptr == ',') {
comma[j++] = i+1;
}
}
ptr = ptrTop;
j1=comma[0];
j2=comma[1] - comma[0];
time_flag = 0;
if(j2>=4) {
strncpy((char *)hour,ptr+j1 ,2);
strncpy((char *)min, ptr+j1+2,2);
strncpy((char *)sec, ptr+j1+4,2);
time_flag |=0x01;
}
j1=comma[1];
j2=comma[2] - comma[1];
if(j2>=1) {
if(*(ptr + j1) == 'A') {
time_flag |=0x04;
}
}
j1=comma[8];
j2=comma[9] - comma[8];
if(j2>=4) {
strncpy((char *)day, ptr+j1 ,2);
strncpy((char *)mon, ptr+j1+2,2);
strncpy((char *)year, ptr+j1+4,2);
time_flag |=0x02;
year = atoi((char *)year);
mon = atoi((char *)mon);
day = atoi((char *)day);
hour = atoi((char *)hour);
min = atoi((char *)min);
sec = atoi((char *)sec);
}
}
}
}
There are a few errors in your code. I have tested it, and with some smaller changes it works as expected.
Add the correct header files:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Please change
if (rx2_id_buff != '\n') {
gps_linebuf[idx++] = rx2_id_buff;
to
if (rx2_id_buff[0] != '\n') {
gps_linebuf[idx++] = rx2_id_buff[0];
The integer output variables need to have different names than the corresponding string variables:
int i_year,i_mon,i_day;
int i_hour,i_min,i_sec;
i_year = atoi((char *)year);
i_mon = atoi((char *)mon);
i_day = atoi((char *)day);
i_hour = atoi((char *)hour);
i_min = atoi((char *)min);
i_sec = atoi((char *)sec);

Why doesn't my program recognize the PORTbits.RCx == 0 condition?

void UART_init(void){
ANSELB = 0; //set PORT B to digital port
TRISBbits.TRISB5 = 1; //set RX pin to input
TRISBbits.TRISB7 = 0; //set TX pin as output
SPBRGH = 0;
SPBRGL = 25; //set baud rate to 9600
BRGH = 0;
BRG16 = 0;
SYNC = 0;
SPEN = 1; //enable serial port pins
TX9 = 0; //set 9 bit tranmission
RX9 = 0; //set 9 bit receive
TXEN = 1; //enable transmission
CREN = 1; //enable receiver
}
void UART_write(char data){
while(TRMT == 0);
TXREG = data;
}
void UART_write_string(char *text){
for(int i=0; text[i] != '\0'; i++){
UART_write(text[i]);
}
}
char UART_read(){
while(RCIF == 0);
return RCREG;
}
char URAT_read_string(char *stringprimit, int lungime){
for(int i=0; i < lungime; i++){
stringprimit[i] = UART_read();
}
}
int main(int argc, char** argv) {
OSCCONbits.IRCF = 0b1111; //set operating frequency to 31kHz (0b1111) for 16MHz
//WDTCONbits.WDTPS = 0b01110;
//CLRWDT();
//0b01100; //set WTD interval at 4s
UART_init();
//Activam pull-up
OPTION_REGbits.nWPUEN = 0;
WPUCbits.WPUC2 = 1;
WPUCbits.WPUC6 = 1;
WPUCbits.WPUC7 = 1;
WPUCbits.WPUC0 = 0;
WPUCbits.WPUC1 = 0;
WPUCbits.WPUC3 = 1;
//Led-uri
TRISAbits.TRISA1 = 0; // set as output
TRISAbits.TRISA2 = 0; // set as output
ANSELAbits.ANSA1 = 0; //pin digital
ANSELAbits.ANSA2 = 0; //pin digital
ANSELAbits.ANSA0 = 1; //set to analogic pin
TRISAbits.TRISA0 = 1; //set as input
ANSELCbits.ANSC0 = 0; //set to digital pin
ANSELCbits.ANSC1 = 0; //set to digital pin
TRISCbits.TRISC0 = 0; //set as output
TRISCbits.TRISC1 = 0; //set as output
ANSELCbits.ANSC2 = 0; //set to digital pin
ANSELCbits.ANSC6 = 0; //set to digital pin
ANSELCbits.ANSC7 = 0; //set to digital pin
TRISCbits.TRISC2 = 1; //set as input
TRISCbits.TRISC6 = 1; //set as input
TRISCbits.TRISC7 = 1; //set as input
PORTCbits.RC0 = 1;
PORTCbits.RC1 = 0;
PORTAbits.RA1 = 0;
PORTAbits.RA2 = 1;
char user_input;
UART_write_string("1. rotate right ");
UART_write('\r');
UART_write_string("2. rotate left");
UART_write('\r');
UART_write_string("3. stop");
UART_write('\r');
UART_write_string("4. Deactivate UART");
UART_write('\r');
UART_write_string("select:");
while(1){
//CLRWDT();
user_input = UART_read();
if(PORTCbits.RC7 == 0 ){ //motor rotates right
user_input = '1';
PORTCbits.RC0 = 1;
PORTCbits.RC1 = 0;
PORTAbits.RA1 = 0;
PORTAbits.RA2 = 1;
}
if(PORTCbits.RC6 == 0 ){ //motor rotates left
user_input = '2';
PORTCbits.RC0 = 0;
PORTCbits.RC1 = 1;
PORTAbits.RA1 = 1;
PORTAbits.RA2 = 0;
}
if(PORTCbits.RC2 == 0 ){ //motor stop
user_input = '3';
PORTCbits.RC0 = 0;
PORTCbits.RC1 = 0;
PORTAbits.RA1 = 0;
PORTAbits.RA2 = 0;
}
if(user_input =='1') {//motor rotates right
PORTCbits.RC0 = 1;
PORTCbits.RC1 = 0;
PORTAbits.RA1 = 0;
PORTAbits.RA2 = 1;
}
if( user_input =='2'){ //motor rotates left
PORTCbits.RC0 = 0;
PORTCbits.RC1 = 1;
PORTAbits.RA1 = 1;
PORTAbits.RA2 = 0;
}
if(user_input =='3'){ //motor stop
PORTCbits.RC0 = 0;
PORTCbits.RC1 = 0;
PORTAbits.RA1 = 0;
PORTAbits.RA2 = 0;
}
if(user_input =='4'){ //deactivate UART
CREN = 0;
}
}
return (EXIT_SUCCESS);
}
In this project I tried to control a DC motor with two methods, the first method is UART with user's input and the second with buttons.
The scope: When I press the numer 1 from the keyboard I want the motor rotates to the right, 2- left, 3 - stop. This works properly, but the problem is with the buttons, when I press the RC7 button nothing happens.
RC7 - means the motor rotates right, RC6 - them motor rotates left, RC2 - the motor stop
From what I can tell, my program doesn't even see those conditions, like PORTbits.RC7 == 0.
From the code, I can see that the function:
char UART_read(){
while(RCIF == 0);
return RCREG;
}
is blocking on while(RCIF == 0);
and I think this is the reason your Buttons are not working because at the very beginning of the while(1) you have called UART_read();.
And you mentioned it yourself that the output is as expected for UART data, so this answers your question.
To make it work, you have to write a UART Rx Interrupt so that your program should not be blocking/waiting on UART to Receive data.
Edit:
As per your comment, that you are disabling the UART reception using CREN, then you should also check if the Receiver is enabled prior to reading the Rx Data register, you can modify the UART receive function as below:
char UART_read(){
if(CREN == 0) {
return '\0'; //Return NULL Character
}
while(RCIF == 0);
return RCREG;
}

Could there be a way to escape this loop?(problem with fade and leds)

int led1 = 3;
int led2 = 5;
int led3 = 6;
int led4 = 9;
int m = 1;
int brightness = 10;
int fadeAmount = 5;
int led;
int one = 1;
void setup()
{
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
}
void loop()
{
if (m == 4 or m == 1) {
one = -one;
}
if (intensite <= 5){
m = m + one;
}
if (m == 1) {
led = led1;
}
if (m == 2) {
led = led2;
}
if (m == 3) {
led = led3;
}
if (m == 4) {
led = led4;
}
brightness = brightness + fadeAmount;
if (brightness <= 5 or brightness >= 255) {
fadeAmount = -fadeAmount;
}
analogWrite(led,brightness);
delay(10);
}
When I run this part of the code, it just gets stuck in a infinite loop at the top led and it does not go back down the way it should which I fail to see. It would be of great help if one could help me understand my mistake. The aim is to make a code that lights leds from right to left (led1 to led4) then left to right (led4 to led1), not simply turning them on but fading them.
I tried my best to stay as close to your code as possible. So your code didn't work because it don't make much sense. Why would you make this one variable? It just complicated all the things. Just put a boolean which indicates whether you are going left or not and change it based on the current value of m. Here is how:
int led1 = 3;
int led2 = 5;
int led3 = 6;
int led4 = 9;
int m = 1;
int brightness = 10;
int fadeAmount = 5;
int led;
bool goingLeft;
void setup()
{
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
}
void loop()
{
if (m == 1) { // If we are on the right side we set goingLeft to true
goingLeft = true;
}else if(m == 4){ // If we are on the left side we set goingLeft to false
goingLeft = false;
}
if (brightness <= 5 && goingLeft){ // if we are moving left then we increment the m variable
m++;
}else if(brightness <= 5){ // if we are moving right we decrement it
m--;
}
if (m == 1) {
led = led1;
}
if (m == 2) {
led = led2;
}
if (m == 3) {
led = led3;
}
if (m == 4) {
led = led4;
}
brightness = brightness + fadeAmount;
if (brightness <= 5 or brightness >= 255) {
fadeAmount = -fadeAmount;
}
analogWrite(led,brightness);
delay(10);
}
If I were you I would do this in a completly diffrent way by using for loops but as I said earlier I tried to stay as close to your code as I could.
Also I saw that you used or in your code. Don't do it!!! In C languages the or operator is || not or

Reading values from an array

I am trying to read an RFID tag and compare the RFID number with a array which I have already there. In my code, the comparing is not working properly. It always gives output as both 'Found' and 'Not found'. Can any one help me on this?
The two RFID numbers that I want to read are: 37376B34 and 7AA29B1A. These two are only for testing. I'm going to store about 20 RFID numbers in the array and check.
My code:
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
int readsuccess;
byte readcard[4];
char str[32] = "";
String StrUID;
char* myTags[] = {"9FF4375C","37376B34","7AA29B1A","1B7D5223","9FF4375C"};
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
}
// --------------------------------------------------------------------
void loop() {
readsuccess = getid();
if (readsuccess) {
Serial.println(StrUID);
delay(1000);
}
for (int i = 0; i < 5; i++) {
if (StrUID == myTags[i]) {
Serial.println("Found");
return;
} else {
Serial.println("Not Found");
}
}
}
// --------------------------------------------------------------------
int getid() {
if (!mfrc522.PICC_IsNewCardPresent()) {
return 0;
}
if (!mfrc522.PICC_ReadCardSerial()) {
return 0;
}
for (int i=0; i<4; i++) {
readcard[i] = mfrc522.uid.uidByte[i]; //storing the UID of the tag in readcard
array_to_string(readcard, 4, str);
StrUID = str;
}
mfrc522.PICC_HaltA();
return 1;
}
// --------------------------------------------------------------------
void array_to_string(byte array[], unsigned int len, char buffer[]) {
for (unsigned int i = 0; i < len; i++) {
byte nib1 = (array[i] >> 4) & 0x0F;
byte nib2 = (array[i] >> 0) & 0x0F;
buffer[i * 2 + 0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA;
buffer[i * 2 + 1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA;
}
buffer[len * 2] = '\0';
}
Data type byte in Arduino is just unsigned char in c++. Since the data that you read from the reader are in bytes, so it is possible to directly compare it with myTags without the need of using String object or even the array to string conversion. You can do the comparison with C++ memcmp() function.
const char* myTags[] = {"9FF4375C","37376B34","7AA29B1A","1B7D5223","9FF4375C"};
byte myReading[4] = {0x9f,0xf4,0x37,0x5c};
for (int i = 0; i < 5; i++) {
if (memcmp(myTags[i], myReading, sizeof(myReading)) == 0) {
Serial.println("Found");
return;
} else {
Serial.println("Not Found");
}
}

Pic programming Using C counter up and down

#include <xc.h>
#include "LCD.h"
#include <stdio.h>
#include <stdlib.h>
int main()
{
unsigned int a; int x = 0; char s[10000];
TRISD = 0x00; TRISB = 0x03;
Lcd_Start();
while(1)
{
Lcd_Clear();Lcd_Set_Cursor(1,1);
int x = 0;
if (PORTBbits.RB0==1)
{
Lcd_Clear();
x += 1;
Lcd_Print_String("Current Number");
Lcd_Set_Cursor(2,1);
Lcd_Print_String(x)
}
else if(PORTBbits.RB1==1)
{
Lcd_Clear();
x += -1;
Lcd_Print_String("Current Number");
Lcd_Set_Cursor(2,1);
Lcd_Print_String(x);
}
}
return 0;
}
This is Lcd_Print_String:
void Lcd_Print_Char(char data) //Send 8-bits through 4-bit mode
{
char Lower_Nibble,Upper_Nibble;
Lower_Nibble = data&0x0F;
Upper_Nibble = data&0xF0;
RS = 1; // => RS = 1
Lcd_SetBit(Upper_Nibble>>4); //Send upper half by shifting by 4
EN = 1;
for (int i=2130483; i<=0; i--) NOP();
EN = 0;
Lcd_SetBit(Lower_Nibble); //Send Lower half
EN = 1;
for (int i=2130483; i<=0; i--) NOP();
EN = 0;
}
void Lcd_Print_String(char *a)
{
int i;
for (i=0;a[i]!='\0';i++)
Lcd_Print_Char(a[i]);
}
I want to display the value of x on the screen, my Lcd_pring_String only takes string type. Is there a way convert x to a string so I can display on LCD?
I am using PIC16f877A, LCD(lm016) and two switches to take signal for +1, -1.
You probably want to use sprintf which is declared in <stdio.h>:
char buffer[20]; // char buffer
sprintf(buffer, "%d", x); // "print" x into the char buffer
Lcd_Print_String(buffer); // send buffer contents to LCD
or instead of sprintf use snprintf if available:
snprintf(buffer, sizeof buffer, "%d", x);
I'd make a new function Lcd_Print_Number out of this.

Resources