Will print and display, on an LCD, 8,16 and 32 bit variables.
My save you a bit of time writing the code yourself?
Works OK - could possibly be polished up a bit?
Code: Select all
////////////////////////////////////////////////////////////////////////////////
// Project: Binary_Numbers //
// File: Binary_Numbers.c //
// Function: Decompose 32,16,8 bit numbers into 0s and 1s for LCD //
// ...display or printing //
// MCU: PIC18F8722 //
// Board: BIGPIC5 //
// Power 5V. //
// Compiler: mikroC PRO for PIC version 7.2.0 //
// Programmer: On-board //
// Author: WVL //
// Date: 1 August 2021 //
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Notes //
// mikroE Libraries to be selected: //
// C_Type, LCD, PrintOut, UART //
// Uses UART 2 and LCD on PORTF with Mikro adaptor //
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Defines and includes //
#define LED0 LATB0_bit // heartbeat flash //
#include "built_in.h" // for Lo and Hi functions //
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// LCD module connections on Mikro adapter //
sbit LCD_RS at RF2_bit; //
sbit LCD_EN at RF3_bit; //
sbit LCD_D4 at RF4_bit; //
sbit LCD_D5 at RF5_bit; //
sbit LCD_D6 at RF6_bit; //
sbit LCD_D7 at RF7_bit; //
sbit LCD_RS_Direction at TRISF2_bit; //
sbit LCD_EN_Direction at TRISF3_bit; //
sbit LCD_D4_Direction at TRISF4_bit; //
sbit LCD_D5_Direction at TRISF5_bit; //
sbit LCD_D6_Direction at TRISF6_bit; //
sbit LCD_D7_Direction at TRISF7_bit; //
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Function - direct PrintOut stream to UART2 //
void Print(char c){UART2_Write(c);} //
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Function - decompose an unsigned byte into 0s and 1s //
void Decompose_char(char byte){ //
unsigned short n; //
for(n=1; n<=8; n++){ //
if((byte & 0x80) !=0)Print(49); //
else Print(48); //
//if (n%8==0)Print(32); // insert a space between bytes //
if(n%4==0) Print(32); // insert space between nibbles //
byte = byte<<1; //
} //
Print(13); // CR at end of printout //
} //
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Function - decompose an unsigned int into 0s and 1s //
void Decompose_int(int my_int){ //
unsigned short n; //
for(n=1; n<=16; n++){ //
if((my_int & 0x8000) !=0)Print(49); //
else Print(48); //
//if (n%8==0)Print(32); // insert a space between bytes //
if(n%4==0) Print(32); // insert space between nibbles //
my_int = my_int<<1; //
} //
Print(13); // CR at end of printout //
} //
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Function - decompose an unsigned long into 0s and 1s //
void Decompose_long(long my_long){ //
unsigned short n; //
for(n=1; n<=32; n++){ //
if((my_long & 0x80000000) !=0)Print(49); //
else Print(48); //
//if (n%8==0)Print(32); // insert a space between bytes //
if(n%4==0) Print(32); // insert space between nibbles //
my_long = my_long<<1; //
} //
Print(13); // CR at end of printout //
} //
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Function - decompose an unsigned byte into 0s and 1s and display on LCD //
void Decompose_char_LCD(char byte){ //
unsigned short n; //
Lcd_Cmd(_LCD_FIRST_ROW); //
for(n=1; n<=8; n++){ //
if((byte & 0x80)!=0)Lcd_Chr_Cp(49); //
else Lcd_Chr_Cp(48); //
//if (n%8==0)Lcd_Chr_Cp(32); // insert a space between bytes //
if(n%4==0) Lcd_Chr_Cp(32); // insert space between nibbles //
byte = byte<<1; //
} //
} //
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Function - decompose an unsigned int into 0s and 1s and display on LCD //
void Decompose_int_LCD(int my_int){ //
unsigned short n; //
Lcd_Cmd(_LCD_SECOND_ROW); //
for(n=1; n<=16; n++){ //
if((my_int & 0x8000)!=0)Lcd_Chr_Cp(49); //
else Lcd_Chr_Cp(48); //
//if (n%8==0)Lcd_Chr_Cp(32); // insert a space between bytes //
if(n%4==0) Lcd_Chr_Cp(32); // insert space between nibbles //
my_int = my_int<<1; //
} //
} //
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Function - decompose an unsigned long into 0s and 1s and display on LCD //
void Decompose_long_LCD(long my_long){ //
unsigned short n; //
Lcd_Cmd(_LCD_THIRD_ROW); //
for(n=1; n<=16; n++){ //
if((my_long & 0x80000000)!=0)Lcd_Chr_Cp(49); //
else Lcd_Chr_Cp(48); //
//if (n%8==0)Lcd_Chr_Cp(32); // insert a space between bytes //
if(n%4==0) Lcd_Chr_Cp(32); // insert space between nibbles //
my_long = my_long<<1; //
} //
Lcd_Cmd(_LCD_FOURTH_ROW); //
for(n=1; n<=16; n++){ //
if((my_long & 0x80000000)!=0)Lcd_Chr_Cp(49); //
else Lcd_Chr_Cp(48); //
//if (n%8==0)Lcd_Chr_Cp(32); // insert a space between bytes //
if(n%4==0) Lcd_Chr_Cp(32); // insert space between nibbles //
my_long = my_long<<1; //
} //
} //
////////////////////////////////////////////////////////////////////////////////
void main() {
// block variables
unsigned short us = 123;
unsigned int ui = 6000;
unsigned long ul = 0xf0f0f0f0;
unsigned short n = 0; // re-usable 8bit
// Init
INTCON2.RBPU = 1; // PORTB pullups disabled
ADCON1 = 0b00001111; // all digital
CMCON = 0b00000111; // disable comparators
EBDIS_bit = 1; // disable memory mode to free PORTD
// Init ports
TRISA = 0b00000000;
TRISB = 0b00000000;
TRISC = 0b00000000;
TRISD = 0b00000000;
TRISE = 0b00000000;
TRISF = 0b00000000;
TRISH = 0b00000000;
TRISJ = 0b00000000;
// Init peripherals
UART1_Init(9600); // UART1 on C6/tx and C7/rx
UART2_Init(9600); // UART2 on G1/tx and G2/rx
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR); // Clear LCD display
Lcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off
// Announce
for(n=0;n<11;n++){LED0 = ~LED0;delay_ms(100);}
Printout(Print,"\r\nBinary Numbers\r\n");
Lcd_Out(1,1,"Binary Numbers");
Lcd_Out(2,1,"On PIC18F8722");
Lcd_Out(3,1,"At 40Mhz");
Lcd_Out(4,1,"July 2021");
delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR); // Clear LCD display
while(1){
Decompose_char(us);
Decompose_char_LCD(us);
Decompose_int(ui);
Decompose_int_LCD(ui);
Decompose_long(ul);
Decompose_long_LCD(ul);
delay_ms(100);
LED0 = ~LED0;
}
}