I'm attempting to write to an LCD (LCD1602 Display Screen) using a PCF8574 IO Expansion Board. I've used some example code I found, but although it does flash the background light (so I know it is communicating with the LCD) it doesn't print numbers.
I don't want to use the WiringPI library because it is no longer supported and I want to use the BCM2835 libraries. Anyone know how I can write characters to the LCD? I thought I only needed to send the ascii codes?
#include <bcm2835.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
char buf[1];
char wbuf[] = "Hello World!";
if (!bcm2835_init())return 1;
bcm2835_i2c_begin(); //Start I2C operations.
bcm2835_i2c_setSlaveAddress(0x27); //I2C address
bcm2835_i2c_set_baudrate(10000); //1M baudrate
buf[0] = 0xEF; //LED ON
bcm2835_i2c_write(buf,1);
int ln = strlen(wbuf);
for (int i=0; i< ln; i++)
{
buf[0] = wbuf[i];
bcm2835_i2c_write(buf,1);
bcm2835_delay(5);
}
bcm2835_i2c_end();
bcm2835_close();
return 0;
}
Rather than doing all the work directly through bcm2835 I discovered a library for speaking to an lcd screen via i2c.
https://github.com/albertherd/LCD1602
If you did want to do this solely through bcm2835 then it looks like you just need to do some file IO operations for the I2C controller on the PI. (easier to use the library)
Related
I am trying to make my first code to a Verifone Device. This device has Verix OS and Arm Processor.
I was following the "OPERATING SYSTEM PROGRAMMING TOOLS REFERENCE MANUAL" and its code works well:
#include <string.h>
#include <svc.h>
char Greeting[] = "Hello!";
void main (void)
{
int display = open(DEV_CONSOLE, O_WRONLY);
write(display, Greeting, strlen(Greeting));
normal_tone();
close(display);
}
however, my code does'nt work well. Why? This and other variations compile, but the entries by keypad does not work.
#include <string.h>
#include <svc.h>
#include <stdio.h>
int main (void)
{
int display = open(DEV_CONSOLE, O_RDWR);
char result[30];
char ent[2]="\0\0";
write(display, "\nEnter a number: ", 17);
read(display, ent, 1);
sprintf(result,"\nPressed key: %s", ent);
write(display, result, strlen(result));
normal_tone();
close(display);
return 0;
}
I have already tried scanf(), fscanf(), getchar(), fgetc(), fgets()... Always happens similars things. Write() and similars sometimes works sometimes not, it depends on the combination with read(), Scanf()...but the read() function and similars never worked on my Verifone device.
int display = open(DEV_CONSOLE, O_WRONLY);
O_WRONLY means write only. Try using O_RDWR instead:
int display = open(DEV_CONSOLE, O_RDWR);
Im following James Molloy`s guide to create small OS and now i stuck on interrupt. I dont really understand how to call my interrupt handlers instead of this command:
asm volatile("int $0x21");
Main file
#include "monitor.h"
#include "multiboot.h"
#include "descriptor_tables.h"
#include "timer.h"
#include "paging.h"
#include "simple.h"
int main(struct multiboot *mboot_ptr){
// Initialise all the ISRs and segmentation
init_descriptor_tables();
// Initialise the screen (by clearing it)
monitor_clear();
monitor_write("Hello, paging world!\n");
init_simple();
asm volatile("int $0x21");
return 0;
}
Where 0x21 is an interrupt`s number in vector.Is there a method to make a interrupt using a c command?
For example i want use this commands:
char c; // for interrupt created and handler allocate memory for char/int etc.
char c = 'a'; // allocate + assing
c; // get value
c = 'b'; // assing new value
Is there any possible way to do it?
Interrupts are generated by the CPU itself.
While C is commonly used to write the interrupt handlers in the kernel, there is no unique facility (or machine model) dictating their creation using it.
I'm attempting to write a simple application in C, I'm fairly new to the concept of C so I apologize if this is very simple. I'm running Windows 7 and have something along the lines of this:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#define Green "\33[0:32m"
#define Yellow "\33[0:33m"
#define Reset "\33[0m"
#define Log_info(X) printf("[Info] %s %s %s\n", Green,X,Reset)
#define Log_warn(X) printf("[Warning] %s %s %s\n",Yellow,X,Reset)
#define Seperator() printf("----------------------------------------------------\n")
void info(const char *message)
{
Log_info(message);
Seperator();
}
void warn(const char *message)
{
Log_warn(message);
Seperator();
}
int main(int argc, char *argv[])
{
warn("test the warning output for the console");
info("test the information output for the console");
}
However when I attempt to run the information handling I get the following:
[Warning] ←[0:33m test the warning output for the console ←[0m
----------------------------------------------------
[Info] ←[0:32m test the information output for the console ←[0m
----------------------------------------------------
What am I doing wrong to the point where it is not color coordinating the output, and instead using the arrows? How can I color coordinate the information, yellow for warnings, green for information?
I got the idea of using \33[0:32m mostly from Javascript (\033[32m #<=Green) and Ruby (\e[32m #<=Green).
You aren't using the right color codes. And these color codes only work on Unix systems with compatible terminals.
Since you want a C and Windows specific solution, I'd recommend using the SetConsoleTextAttribute() function in the Win32 API. You'll need to grab a handle to the console, and then pass it with the appropriate attributes.
As a simple example:
/* Change console text color, then restore it back to normal. */
#include <stdio.h>
#include <windows.h>
int main() {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
WORD saved_attributes;
/* Save current attributes */
GetConsoleScreenBufferInfo(hConsole, &consoleInfo);
saved_attributes = consoleInfo.wAttributes;
SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE);
printf("This is some nice COLORFUL text, isn't it?");
/* Restore original attributes */
SetConsoleTextAttribute(hConsole, saved_attributes);
printf("Back to normal");
return 0;
}
For more info on the available attributes, look here.
I need a simple program that will write to gpio. I can't find one anywhere. The example in the mmra documentation does not work. I picked gpio14 because the Sprakfun example that writes to this pin using system calls works just fine. But my program does not work.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mraa.h>
#include <math.h>
#include <mraa/gpio.h>
int main(int argc, char **argv)
{
mraa_gpio_context gpio;
gpio = mraa_gpio_init(14); <--- to get gpio14 to toggle change this to 36
mraa_gpio_dir(gpio, MRAA_GPIO_OUT);
int value = 0;
for (;;) {
if(value == 0)value = 1;
else value = 0;
mraa_gpio_write(gpio,value);
printf("output is %d\n",value);
sleep(1);
}
mraa_gpio_close(gpio);
return 0;
}
The loop runs and prints out output is 1 then output is 0. I have an oscilloscope on the pin and it stays low.
Using this Sparkfun tutorial I can move the pin high so I know my setup is correct. If someone can just give me an example of code that works with all the includes and such that would be very helpful.
It turns out that the code I posted is correct and works. The problem is that gpio14 is mraa 36 so to toggle gpio14, I need to change the number in init to 36. Here is the defining document.
mraa decoder ring
I am trying to read data from a bluetooth barcode scanner (KDC300) using C. Here is the code I have so far, and the program successfully establishes a bluetooth connection to the scanner, but when a barcode is scanned, no input is displayed on the screen (Eventually more will be done with the data, but we have to get it working first, right).
Here is the program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <sys/ioctl.h>
int main (int argc, const char * argv[]) {
// define vars
int STOP = 0;
//char buf[255];
if(argv[1])
{
int fd = open("/dev/tty.KDC1", O_RDONLY);
if(fd == -1)
{
printf("%s", strcat("Unable to open /dev/tty.", argv[1]));
}
int res;
while(STOP == 0)
{
while((res = read(fd,buf,255)) == 0);
{
if(res > 0)
{
buf[res]=0;
printf("%s:%d\n", buf, res);
if(buf[sizeof(buf)]=='\n') break;
}
}
}
}
return 0;
}
If anyone has any ideas, I am at a loss on this so far. If it is any help, I can run screen /dev/tty.KDC1 and any barcodes scanned on the scanner appear in the terminal, I just can't do anything with the data.
Jud
This line:
while((res = read(fd,buf,255)) == 0);
Does not do what you think it does. That's a while loop with an empty body.
#tommieb75,
the strcat statement was from the first "go" at the program, I took a variable from argv[1] and appended it to the /dev/tty.* so you could select which device you wanted to monitor.
I am not sure why I had commented out buf, probably stems from looking at the code too much / trying different approaches and forgetting where I was (not much of a C programmer, which is how I can get lost in 30 LOC).
#caf, Good catch on the extra semi-colon after the while loop, unfortunately, even after correcting it, the program doesn't behave correctly.
I am researching the problem further. I can verify (with osx packetlogger) that the computer is getting the data, but the but the buffer never has any data placed in it.
-Jud
---------------Edit--------------
I solved the problem after a little trial and error. Adding the following code to setup the serial connection solved everything:
struct termios theTermios;
memset(&theTermios, 0, sizeof(struct termios));
cfmakeraw(&theTermios);
cfsetspeed(&theTermios, 115200);
theTermios.c_cflag = CREAD | CLOCAL; // turn on READ
theTermios.c_cflag |= CS8;
theTermios.c_cc[VMIN] = 0;
theTermios.c_cc[VTIME] = 10; // 1 sec timeout
ioctl(fileDescriptor, TIOCSETA, &theTermios);
Thanks to the other answers for getting me to this point.
Here is the best info I've found.
The C program on there using termios worked just by adding
#include<string.h>
And changing the baudrate to match my needs.
In your code
printf("%s", strcat("Unable to open /dev/tty.", argv[1]));
Why did you do that? It would be easier to do it this way:
printf("%s: Unable to open /dev/tty.KDC1", argv[0]);
Why the parameter referencing to the command line?
res = read(fd,buf,255)
Why did you have buf declaration commented out above?