I'm making one-buttoned bandit slot machine made on 3x 7 segment displays and 3x 74HC595 Shift Registers. Problem is that I want to have some kind of indicator that you won (or you partially won - 2 numbers are the same).
losLiczb(latchPin3, dataPin3, clockPin3, data3, liczba3); // liczba3 = in eg. 4
Serial.println("Wylosowana liczba to"); // "Generated number:"
Serial.println(liczba3); // liczba3 = 0
So, liczba1/liczba2/liczba3 is number which was generated in function losLiczb. Main problem is that liczba1/2/3 have assigned number inside function losLiczb (I can chceck correctness via Serial Read), but outside the number liczba1/2/3 is 0, thus program will always indicate that you won. Here is the whole program if you want (some of comments are in polish and are unnessecary,:
int latchPin1 = 5; //Pin connected to ST_CP of 74HC595
int clockPin1 = 4; //Pin connected to SH_CP of 74HC595
int dataPin1 = 6; //Pin connected to DS of 74HC595
int latchPin2 = 8;
int clockPin2 = 7;
int dataPin2 = 9;
int latchPin3 = 11; //11
int clockPin3 = 12; //12
int dataPin3 = 10; //8
int liczba1;
int liczba2;
int liczba3;
const int buttonPin = 2;
int statusPin = 3;
int buttonState = 0;
//holders for infromation you're going to pass to shifting function
byte data;
byte data1;
byte data2;
byte data3;
byte dataArray[10];
byte animacja[8];
byte dummy[1];
void setup() {
//set pins to output because they are addressed in the main loop
pinMode(latchPin1, OUTPUT);
pinMode(latchPin2, OUTPUT);
pinMode(latchPin3, OUTPUT);
pinMode(statusPin, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
//Arduino doesn't seem to have a way to write binary straight into the code
//so these values are in HEX. Decimal would have been fine, too.
// 00000000 - trzeba zapisywać w translatorze hex-bin w takiej postaci.
// Kolejno od prawej wartości odpowiadają kropce, a, b , c , d, e , f , g
// dataArray[2] = 0xB6; //11111100 FC
dataArray[0] = 0x7F;
dataArray[1] = 0x0C;
dataArray[2] = 0xB6;
dataArray[3] = 0x9E;
dataArray[4] = 0xCC;
dataArray[5] = 0xDA;
dataArray[6] = 0xFA;
dataArray[7] = 0x0E;
dataArray[8] = 0xFE;
dataArray[9] = 0xDE;
animacja[0] = 0x80; //g
animacja[1] = 0x20; //e
animacja[2] = 0x10; //d
animacja[3] = 0x08; //c
animacja[4] = 0x80; //g
animacja[5] = 0x40; //f
animacja[6] = 0x02; //a
animacja[7] = 0x04; //b
dummy[0] = 0x00;
randomSeed(analogRead(0));
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
anim();
} else {
idle();
digitalWrite(statusPin, LOW);
}
}
void idle(){
data = dummy[0];
digitalWrite(latchPin1, 0);
shiftOut(dataPin1, clockPin1, data);
digitalWrite(latchPin1, 1);
digitalWrite(latchPin2, 0);
shiftOut(dataPin2, clockPin2, data);
digitalWrite(latchPin2, 1);
digitalWrite(latchPin3, 0);
shiftOut(dataPin3, clockPin3, data);
digitalWrite(latchPin3, 1);
}
// Funkcja odpowiedzialna za animację
void anim(){
for (int p =0; p < 3; p++){
for (int i = 0; i < 8; i++) {
data = animacja[i];
digitalWrite(latchPin1, 0);
shiftOut(dataPin1, clockPin1, data);
digitalWrite(latchPin2, 0);
shiftOut(dataPin2, clockPin2, data);
digitalWrite(latchPin3, 0);
shiftOut(dataPin3, clockPin3, data);
digitalWrite(latchPin1, 1);
digitalWrite(latchPin2, 1);
digitalWrite(latchPin3, 1);
delay(100);
}
}
losLiczb(latchPin1, dataPin1, clockPin1, data1, liczba1);
Serial.println("Wylosowana liczba to");
Serial.println(liczba1);
for (int p =0; p < 2; p++){
for (int i = 0; i < 8; i++) {
data = animacja[i];
digitalWrite(latchPin2, 0);
shiftOut(dataPin2, clockPin2, data);
digitalWrite(latchPin3, 0);
shiftOut(dataPin3, clockPin3, data);
digitalWrite(latchPin2, 1);
digitalWrite(latchPin3, 1);
delay(100);
}
}
losLiczb(latchPin2, dataPin2, clockPin2, data2, liczba2);
Serial.println("Wylosowana liczba to");
Serial.println(liczba2);
for (int p =0; p < 2; p++){
for (int i = 0; i < 8; i++) {
data = animacja[i];
digitalWrite(latchPin3, 0);
shiftOut(dataPin3, clockPin3, data);
digitalWrite(latchPin3, 1);
delay(100);
}
}
losLiczb(latchPin3, dataPin3, clockPin3, data3, liczba3);
Serial.println("Wylosowana liczba to");
Serial.println(liczba3);
if (data1 == data2){
digitalWrite(statusPin, HIGH);
} else {
digitalWrite(statusPin, LOW);
}
delay(5000);
}
//Funckja odpowiedzialna za wylosowanie liczby
int losLiczb(int myLatchPin, int myDataPin, int myClockPin, int myData, int myLiczba) {
//load the light sequence you want from array
int j = random(0, 9);
myLiczba = j;
myData = dataArray[j];
//ground latchPin and hold low for as long as you are transmitting
digitalWrite(myLatchPin, 0);
//move 'em out
shiftOut(myDataPin, myClockPin, myData);
//return the latch pin high to signal chip that it
//no longer needs to listen for information
digitalWrite(myLatchPin, 1);
Serial.println("Wylosowana liczba to");
Serial.println(myLiczba);
}
// the heart of the program
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
// This shifts 8 bits out MSB first,
//on the rising edge of the clock,
//clock idles low
//internal function setup
int i=0;
int pinState;
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, OUTPUT);
//clear everything out just in case to
//prepare shift register for bit shifting
digitalWrite(myDataPin, 0);
digitalWrite(myClockPin, 0);
//for each bit in the byte myDataOut�
//NOTICE THAT WE ARE COUNTING DOWN in our for loop
//This means that %00000001 or "1" will go through such
//that it will be pin Q0 that lights.
for (i=7; i>=0; i--) {
digitalWrite(myClockPin, 0);
//if the value passed to myDataOut and a bitmask result
// true then... so if we are at i=6 and our value is
// %11010100 it would the code compares it to %01000000
// and proceeds to set pinState to 1.
if ( myDataOut & (1<<i) ) {
pinState= 1;
}
else {
pinState= 0;
}
//Sets the pin to HIGH or LOW depending on pinState
digitalWrite(myDataPin, pinState);
//register shifts bits on upstroke of clock pin
digitalWrite(myClockPin, 1);
//zero the data pin after shift to prevent bleed through
digitalWrite(myDataPin, 0);
}
//stop shifting
digitalWrite(myClockPin, 0);
}
Pass the values you want the function to output by reference or by pointer.
void Wrong(int a) {
a = 10;
}
void Right(int& a) {
a = 20;
}
void Right(int* pA) {
*pA = 30;
}
int a = 0;
Wrong(a); printf("%d ", a);
Right(a); printf("%d ", a);
Right(&a); printf("%d\n", a);
output will be 0 20 30
Related
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?
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;
}
#include <stdio.h>
#include <string.h>
#include "iodefine.h"
#include "common.h"
#include "config.h"
#include "system.h"
#include "sfr.h"
#include "lcd_con.h"
extern unsigned char TITL0[32] = "DISPLAYtest 2021";
extern unsigned char TITL1[32] = ">Welcome>Home";
extern unsigned char TITL0[32] = "DISPLAYtest 2020";
extern unsigned char TITL1[32] = ">Welcome>EARTH";
extern unsigned char TITL0[32] = "DISPLAYtest 2023";
extern unsigned char TITL1[32] = ">Welcome>EARTh";
void dispset_titl(void) {
unsigned char n;
unsigned char dsp_buf;
lcd_l1(0x00);
for (n = 0; n < 16; n++) {
dsp_buf = TITL0[n]; // line 1
lcd_dout(dsp_buf); // 1data write
}
lcd_l2(0x40);
for (n = 0; n < 16; n++) {
dsp_buf = TITL1[n]; // line 2
lcd_dout(dsp_buf); // 1data write
}
}
void dispset_count(void) {
delay_msec(100);
dpcnt[0]++;
if (dpcnt[0] > 9) {
dpcnt[0] = 0;
dpcnt[1]++;
}
if (dpcnt[1] > 9) {
dpcnt[1] = 0;
dpcnt[2]++;
}
if (dpcnt[2] > 9) {
dpcnt[2] = 0;
dpcnt[3]++;
}
if (dpcnt[3] > 9) {
dpcnt[3] = 0;
}
lcd_cout(0x8C);
lcd_dout(dpcnt[3] + 0x30);
lcd_dout(dpcnt[2] + 0x30);
lcd_dout(dpcnt[1] + 0x30);
lcd_dout(dpcnt[0] + 0x30);
}
void lcd_init(void) {
P0 = 0x00; // E=0,R/W=0
P2 = 0x03;
P0 = 0x02; // E=1,R/W=0
delay_micro(100);
P0 = 0x00; // E=0,R/W=0
delay_msec(5);
P2 = 0x03;
P0 = 0x02; // E=1,R/W=0
delay_micro(100);
P0 = 0x00; // E=0,R/W=0
delay_msec(5);
P2 = 0x03;
P0 = 0x02; // E=1,R/W=0
delay_micro(100);
P0 = 0x00; // E=0,R/W=0
delay_msec(5);
P2 = 0x02;
P0 = 0x02; // E=1,R/W=0
delay_micro(100);
P0 = 0x00; // E=0,R/W=0
delay_msec(5);
lcd_cout(0x28); // for 4-bit lcd intialization
delay_msec(5);
lcd_cout(0x0F);
delay_msec(5);
lcd_cout(0x06);
delay_msec(5);
lcd_cout(0x01);
delay_msec(5);
lcd_cout(0x02);
delay_msec(5);
}
void lcd_l1(unsigned char column) {
unsigned char pos;
pos = column & 0x0F;
pos = pos | 0x80;
lcd_cout(pos);
delay_msec(5);
}
void lcd_l2(unsigned char column) {
unsigned char pos;
pos = column & 0x4F;
pos = pos | 0xC0;
lcd_cout(pos);
delay_msec(5);
}
void lcd_cout(unsigned char code) {
unsigned char code_msb, code_lsb;
code_msb = code / 0x10;
code_lsb = code & 0x0F;
P0 = 0x00; // E=0,R/W=0
P2 = code_msb;
P0 = 0x02; // E=1,R/W=0
delay_micro(2);
P0 = 0x00; // E=0,R/W=0
delay_micro(2);
P2 = code_lsb;
P0 = 0x02; // E=1,R/W=0
delay_micro(2);
P0 = 0x00; // E=0,R/W=0
delay_micro(50);
}
void lcd_dout(unsigned char code) {
unsigned char code_msb, code_lsb;
code_msb = code / 0x10;
code_lsb = code & 0x0F;
P0 = 0x01; // E=0,R/W=1
P2 = code_msb;
P0 = 0x03; // E=1,R/W=1
delay_micro(2);
P0 = 0x01; // E=0,R/W=1
delay_micro(2);
P2 = code_lsb;
P0 = 0x03; // E=1,R/W=1
delay_micro(2);
P0 = 0x01; // E=0,R/W=1
delay_micro(50);
}
void delay_micro(unsigned int icnt) {
unsigned int del_cnt;
for (del_cnt = 0; del_cnt < icnt; del_cnt++) {
NOP();
NOP();
NOP();
NOP();
NOP();
}
}
void delay_msec(unsigned int icnt) {
unsigned int del_cnt;
for (del_cnt = 0; del_cnt < icnt; del_cnt++) {
delay_micro(1000);
}
}
#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.
I am getting an error when reading from the mcp3008 ADC. The function below returns 127 when receiving a 3.3V input. Since it is a 10 bit ADC, it should return 1023.
I have defined my pin numbers correctly, and included both stdio.h and wiringPi.h. Any help is appreciated.
int main(void) {
int adcValue = 0;
wiringPiSetup();
pinMode(csPin, OUTPUT);
pinMode(sclkPin, OUTPUT);
pinMode(mosiPin, OUTPUT);
pinMode(misoPin, INPUT);
while(1) {
delay(10);
adcValue = readADC(CHAN0);
if (adcValue == -1)
printf("Error reading from %i \n", CHAN0);
else
printf("Chan0: %i \n", adcValue);
}
}
int readADC(int adcChan) {
// Variables
unsigned char dataOut = 0;
unsigned char adcout = 0;
int i, j = 0;
if ((adcChan > 7) || (adcChan < 0)) {
return -1;
}
digitalWrite(csPin, HIGH);
digitalWrite(sclkPin, LOW);
digitalWrite(csPin, LOW);
dataOut = adcChan;
dataOut |= 0x18;
dataOut <<= 3;
for (i = 0; i < 5; i++) {
if (dataOut & 0x80) {
digitalWrite(mosiPin, HIGH);
} else {
digitalWrite(mosiPin, LOW);
}
dataOut <<= 1;
digitalWrite(sclkPin, HIGH);
digitalWrite(sclkPin, LOW);
}
adcout = 0;
for (j = 0; j < 12; j++) {
digitalWrite(sclkPin, HIGH);
digitalWrite(sclkPin, LOW);
adcout <<= 1;
if (digitalRead(misoPin)) {
adcout |= 0x1;
}
}
digitalWrite(csPin, HIGH);
adcout >>= 1;
return adcout;
}
Your adcout is a char and can't hold the 10-bit value. Use an unsigned int instead. Also, you should not shift back your value with adcout >>= 1;, it's already good.
You are returning a variable unsigned char adcout = 0; which of a size 8 bits NOT 10 bits. Also you are right shifting it by 1 before returning,So most probably you are getting :
255 Right shifted by 1 = 127.
use python and spidev module!
It will look something like!
import spidev
spi = spidev.SpiDev()
spi.open(0,0)
def read(ch):
adc = (spi.xfer2([1,(8+ch)<<4,0])
data = ((adc[1]&3 << 8) + adc[2]
This should give u proper readings from the mcp3008 chip.
I am currently using this chip in a sensor module for connecting my analog sensors. Ch is the channel on the mcp3008 (0-7)