Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
3v0 I'm with you kind of bad ideal but a lot of them compilers force you to buy there debugger
DAG!
Code:#if ENDIAN = LOWER BF = LOWEND; #else BF = UPPEND; #endif [B][COLOR="Red"] .....produces... D:\Micro\PIC\LCD_LIB\lcd.c:6:Error [1029] malformed expression in '#if'[/COLOR][/B]
So i guess i cant
#define Upper 0
#define Lower 1
#define Endian Lower
#if Endian==Lower
//some code
#else
//some other code
#endif
I have a thing about compiler vendors that require you to buy their debugger. It is a stupid money grubbing thing to do. Sure you can use a simulator but why should you have to when you can get a decent ICD like the PICkit2 for $40 ?
You have to buy there debuggerThey DON'T make you buy the debugger. At least according to their compiler download page the "fully functional free demo version" has only one limitation; "Note: in Demo version, hex output is limited to 2k of program words."
The mikroICD is a highly effective tool for a Real-Time debugging on hardware level. The mikroICD debugger enables you to execute the mikroBasic PRO for PIC 2009 program on a host PIC microcontroller and view variable values, Special Function Registers (SFR), RAM, CODE and EEPROM memory along with the mikroICD code execution on hardware.
If you have appropriate hardware and software for using mikroICD then you have to upon completion of writing your program to choose between Release build Type or ICD Debug build type.
//THIS IS lcd.h
#ifndef __LCD_H
#define __LCD_H
#include <delays.h>
#include <p18cxxx.h>
#define ENDIAN LOWER //0 = 0:3 *** 1 = 4:7
#define LCD_PORT LATB //LCD DATA PORT
#define LCD_TRIS TRISB //LCD DATA DIRECTION PORT
#define LCD_DAT PORTB //LCD INPUT DATA VALUES
#define LCD_RS LATBbits.LATB4 //LCD Command/Data Control
#define LCD_E LATBbits.LATB5 //LCD Enable Line
//#define LCD_RW LATBbits.LATB6 //LCD Read/Write Control
#define LCD_TRIS_RS TRISBbits.TRISB4
#define LCD_TRIS_E TRISBbits.TRISB5
//#define LCD_TRIS_RW TRISBbits.TRISB4
#define CMD 0
#define TXT 1
#define LOWER 0
#define UPPER 1
#define LOWEND 0x08
#define UPPEND 0x80
#define LOWMSK 0x0F
#define UPPMSK 0xF0
#define DISPLAY 1
#define CURSOR 0
#define RIGHT 1
#define LEFT 0
#define MHz *1000000
#define MYFOSC 8MHz
void WaitLCDBusy(void);
void LCD_Init(void);
void LCD_DATA(unsigned char data,unsigned char type);
void LCD_NYB(unsigned char nyb);
void LCD_STR(unsigned rom char *text);
void LCD_LINE(char line);
void ShiftDisplay(unsigned char DC,unsigned char RL);
void DelayMS(unsigned int ms);
extern unsigned char BF;
#endif
//THIS IS lcd.c
#include "lcd.h"
unsigned char BF;
//--------------------------------------------------------------------------------//
void LCD_Init(void){
#if ENDIAN==LOWER //This will check if ENDIAN is LOW and if so
BF = LOWEND; //Set the BusyFlag MASK to check lower nibble
#else //if not LOW, but HIGH(UPPER) then
BF = UPPEND; //Set BF to check upper nibble
#endif
#if ENDIAN==LOWER
LCD_TRIS &= UPPMSK; //ensure lower nibble is output
#else
LCD_TRIS &= LOWMSK; //ensure upper nibble is output
#endif
LCD_E=0; //clear enable
LCD_RS = 0; //going to write command
LCD_TRIS_E=0; //Set enable to output
LCD_TRIS_RS=0; //set RS to output
#ifdef LCD_RW
LCD_TRIS_RW=0;
LCD_RW=0;
#endif
DelayMS(30); //delay for LCD to initialise.
LCD_NYB(0x30); //Required for initialisation
DelayMS(5); //required delay
LCD_NYB(0x30); //Required for initialisation
DelayMS(1); //required delay
LCD_DATA(0x02,0); //set to 4 bit interface, 1 line and 5*7 font
LCD_DATA(0x28,0); //set to 4 bit interface, 2 line and 5*10 font
LCD_DATA(0x0c,0); //set to 4 bit interface, 2 line and 5*7 font
LCD_DATA(0x01,0); //clear display
LCD_DATA(0x06,0); //move cursor right after write
}
//--------------------------------------------------------------------------------//
void LCD_DATA(unsigned char data,unsigned char type){
WaitLCDBusy(); //TEST LCD FOR BUSY
if(type == CMD){
LCD_RS = 0; //COMMAND MODE
} else {
LCD_RS = 1; //CHARACTER/DATA MODE
}
LCD_NYB(data>>4); //WRITE THE UPPER NIBBLE
LCD_NYB(data); //WRITE THE LOWER NIBBLE
}
//--------------------------------------------------------------------------------//
void WaitLCDBusy(void){
#ifdef LCD_RW //IF THE LCD_RW DEFINITION IS USED
LCD_RS=0; //COMMAND MODE
LCD_RW=1; //READ MODE
#if ENDIAN==LOWER //ENDIAN = 0?
LCD_TRIS |= LOWMSK; //YES = SET LOWER NIBBLE INPUT
#else //ELSE ENDIAN = 1
LCD_TRIS |= UPPMSK; //SET UPPER NIBBLE INPUT
#endif
LCD_E=1; //ENABLE LCD DATA LINES
while(LCD_DAT & BF); //TEST BUSY FLAG BIT
LCD_E=0; //DISABLE LCD DATA LINES
#if ENDIAN==LOWER //ENDIAN = 0?
LCD_TRIS &= UPPMSK; //YES = SET LOWER NIBBLE OUTPUT
#else //ELSE ENDIAN = 1
LCD_TRIS &= LOWMSK; //SET UPPER NIBBLE OUTPUT
#endif
LCD_RW=0;
#else
DelayMS(2); //DELAY 1 MilliSeconds
#endif
}
//--------------------------------------------------------------------------------//
void LCD_NYB(unsigned char nyb){
#if ENDIAN==LOWER //ENDIAN = 0? Yes Send the data LOWER
LCD_PORT &= UPPMSK; //CLEAR LOWER PORT NIBBLE
LCD_PORT |= (nyb & LOWMSK); //SEND DATA LINE THE INFO
#else //ENDIAN = 1 so send to HIGHER
LCD_PORT &= LOWMSK; //CLEAR UPPER PORT NIBBLE
LCD_PORT |= ((nyb << 4) & UPPMSK); //SHIFT DATA OVER << 4, CLEAR LOWER NIBBLE
#endif
LCD_E = 1; //ENABLE LCD DATA LINE
Nop(); //SMALL DELAY
LCD_E = 0; //DISABLE LCD DATA LINE
}
//--------------------------------------------------------------------------------//
void LCD_STR(unsigned rom char *text){
while(*text){
LCD_DATA(*text++,1);
}
}
//--------------------------------------------------------------------------------//
void LCD_LINE(char line){
switch(line){
case 0:
case 1:
LCD_DATA(0x80,0);
break;
case 2:
LCD_DATA(0xC0,0);
break;
}
}
//--------------------------------------------------------------------------------//
void ShiftDisplay(unsigned char DC,unsigned char RL){
unsigned char shift=0x10;
if(DC == DISPLAY)
shift |= 0b00001000;
if(RL == RIGHT)
shift |= 0b00000100;
LCD_DATA(shift,0);
}
//--------------------------------------------------------------------------------//
void DelayMS(unsigned int ms){
unsigned int x;
for(x=0;x<ms;x++)
Delay1KTCYx(2);
}
//--------------------------------------------------------------------------------//
Mr RB said:They DON'T make you buy the debugger....
Everyone is loyal to their favorite compiler and dev board but for speed/versatility and value for money the combination above is hard to beat.
You have to buy there debugger
void main(){
char oldstate = 0;
TRISB = 0xFF; // set PORTB to be input
TRISD = 0; // set PORTD to be output
PORTD = 0x0F; // initialize PORTD
do {
if (Button(&PORTB, 1, 1, 1)) // detect logical one on RB1 pin
oldstate = 1;
if (oldstate && Button(&PORTB, 1, 1, 0)) { // detect one-to-zero transition on RB1 pin
PORTD = ~PORTD; // negates value on PORTD
oldstate = 0;
}
} while(1); // endless loop
}
if(button(&PORTB,1,1,1)) blah;
if(PORTB.F1) blah;