|
|
View previous topic :: View next topic |
Author |
Message |
William
Joined: 11 Sep 2010 Posts: 8
|
LCD 16x2 with PIC16F887 |
Posted: Sat Sep 11, 2010 3:57 am |
|
|
Hi I have read all of the posts on the LCD problems, I've used Flex_lcd and the normal LCD library, this is my first project working with CCS and I know I'm just doing something dumb, I've been stuck on this for a few days now and I was hoping that someone could help me! The pins are right and I have triple checked the contrast voltage. I'm using a MC1602C-syl LCD 16x2 but all that I can get are black squares.
Here is the code from the flex_lcd();
Code: |
#define LCD_DB4 PIN_D4
#define LCD_DB5 PIN_D5
#define LCD_DB6 PIN_D6
#define LCD_DB7 PIN_D7
#define LCD_E PIN_D0
#define LCD_RS PIN_D2
#define LCD_RW PIN_D3
// If you only want a 6-pin interface to your LCD, then
// connect the R/W pin on the LCD to ground, and comment
// out the following line.
#define USE_LCD_RW 1
|
and here is all my code:
Code: |
#include <16F887.H>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOLVP, XT
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#include <input.c>
#include <string.h>
#include "flex_lcd.c"
//====================================
#define FOUR_PHASE TRUE
#define LOWTOHIGH TRUE
#byte CCP1CON = 0x17
#byte CCP2CON = 0x1D
#byte PWM1CON = 0x9B
#BYTE port_b = 6
BYTE const POSITIONS[4] = {0b0101,
0b1001,
0b1010,
0b0110};
boolean new = false;
char c = ' ';
int pos = -1;
int currentpos = 0;
char d = ' ';
int speed = -1;
#int_rda
void serial_isr() {
c=getc();
printf(" %c ",c);
lcd_putc(c);
if(c=='q')
new = true;
if((c != '1') && (c != '2') && (c != '3') && (c != '4') && (c != 'r') && (c != 'f') && (c != 'q')){
printf( "WRONG INPUT");
c = ' ';
}
}
void stepper_motor_f(int steps)
{
BYTE k,t;
for(t = 0; t <=abs(currentpos);t++)
{
for(k = 0;k<=steps;k++)
{
static BYTE state = 0;
delay_ms(10);
set_tris_b(0xf0);
port_b = POSITIONS[ state ];
state++;
if (state == 5) state = 0;
}
}
currentpos = pos;
}
void stepper_motor_r(int steps)
{
BYTE k,t;
for(t = 0; t <=abs(currentpos);t++)
{
for(k = 0;k<=steps;k++)
{
static BYTE state = 4;
delay_ms(10);
set_tris_b(0xf0);
port_b = POSITIONS[ state ];
state--;
if (state == 0) state = 4;
}
}
currentpos = pos;
}
void DC_motor_r()
{
int16 pwm_r;
int k = 0;
output_low(PIN_A1);
output_low(PIN_A2);
setup_ccp2(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 128, 1);
pwm_r = speed;
while(k == 0)
{
set_pwm2_duty(pwm_r);
output_high(PIN_A0);
output_high(PIN_A3);
if(new) k = 1;
printf(" Running: ");
delay_ms(1000);
}
output_low(PIN_A0);
set_pwm2_duty(0);
output_low(PIN_A1);
output_low(PIN_A2);
output_low(PIN_A3);
}
void DC_motor_f()
{
int16 pwm_l;
int k = 0;
output_low(PIN_A0);
output_low(PIN_A3);
setup_ccp2(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 128, 1);
pwm_l = speed;
while(k == 0)
{
set_pwm2_duty(pwm_l);
output_HIGH(PIN_A1);
output_HIGH(PIN_A2);
if(new) k = 1;
printf(" Running: ");
delay_ms(1000);
}
output_low(PIN_A0);
set_pwm2_duty(0);
output_low(PIN_A1);
output_low(PIN_A2);
output_low(PIN_A3);
}
void main()
{
enable_interrupts(global);
enable_interrupts(int_rda);
printf("Booting...");
delay_ms(50);
lcd_init();
delay_ms(50);
lcd_putc("\fHello World\n");
lcd_putc("Line Number 2");
while(1)
{
printf(" Please Select a fan position(1 - 4): ");
new = false;
do
{
if (c != ' ')
{
if(c == '1') pos = 1;
if(c == '2') pos = 2;
if(c == '3') pos = 3;
if(c == '4') pos = 4;
currentpos = pos - currentpos;
if (currentpos < 0)
{
stepper_motor_r(12);
printf("Step Back ");
c = ' ';
}else
if (currentpos > 0)
{
stepper_motor_f(14);
printf("Step Forward ");
c = ' ';
}
}
}while(pos == -1);
printf(" Please Select a fan direction(r/f): ");
do
{
if(c == 'r') d = 'r';
if(c == 'f') d = 'f';
}while(d == ' ');
printf(" Please Select a fan speed(1 - 4): ");
do{
if(c == '1') speed = 400;
if(c == '2') speed = 250;
if(c == '3') speed = 180;
if(c == '4') speed = 128;
}while(speed == -1);
if (d == 'r')
{
DC_motor_r();
printf("DCr ");
}else
if (d == 'f')
{
DC_motor_f();
printf("DCf ");
}
d = ' ';
c = ' ';
pos = -1;
speed = -1;
}
}
|
I have also tested the LCD with a basic program and i get the same result. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Sat Sep 11, 2010 4:18 am |
|
|
Are you sure your serial receive line is pulled high at the PIC?.
Problem is that in the ISR for the serial, you output a minimum of 3 characters, and up to eleven characters. If the line is not pulled high, you will get garbage characters received, and this will then hang the UART (you have time to send just _one_ character in the ISR without this happening....). Add 'ERRORS' to your RS232 declaration to avoid the hang, and re-design the code so it won't happen....
Realistically, start your debugging without most of the code. Just use a routine to send some simple text to the LCD, and get this going _before_ trying to build your whole program. This is a 'key' parts to software development - get individual parts working first...
Check the LCD connections again, looking particularly for things like shorts between the connections...
Best Wishes |
|
|
MikeP
Joined: 07 Sep 2003 Posts: 49
|
|
Posted: Sat Sep 11, 2010 5:34 am |
|
|
I am guessing you really want to use the built in clock change the following line to this.
Code: |
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOLVP
|
|
|
|
William
Joined: 11 Sep 2010 Posts: 8
|
|
Posted: Sun Sep 12, 2010 1:39 am |
|
|
Thanks for the advice. I'm looking into those problems. As for the LCD here is the code I'm using at the moment but its still not working. I have a Contrast voltage of 0.4 all the pins are right. I even re-soldered them.
Test code:
Code: |
#include <16F887.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#include "flex_lcd.c"
//==========================
void main()
{
lcd_init(); // Always call this first.
lcd_putc("\fHello World\n");
lcd_putc("Line Number 2");
while(1);
}
|
Here are the datasheets for the pic and the LCD. I don't know what I'm doing wrong.
LCD: http://uk.farnell.com/everbouquet/mc1602c-syr/lcd-module-alphanumeric-2x16-stn/dp/1220424
Pic: http://ww1.microchip.com/downloads/en/DeviceDoc/41291E.pdf[/code] |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Sun Sep 12, 2010 7:20 am |
|
|
So, you have got a 4MHz crystal connected to the PIC (with load capacitors)?.
If not, this won't work.
Best Wishes |
|
|
William
Joined: 11 Sep 2010 Posts: 8
|
|
Posted: Sun Sep 12, 2010 12:49 pm |
|
|
No I don't have one connected, is that the "XT" in the fuses list? I'm really new to a lot of this stuff, sorry. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Sun Sep 12, 2010 2:14 pm |
|
|
Exactly.
You had the internal oscillator selected, and the external crystal (which can result in a potentially impossible setup...), then removed the INTRC_IO fise (which says use the internal oscillator, and assign the external pins for IO), and have left the 'XT' fuse (which says I have an external crystal connected, not faster than 4MHz). With this (and 4MHz selected for the clock) you need a 4MHz crystal on the oscillator pins, with it's correct load capacitors.
If you haven't got a crystal, change your fuses to:
fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOLVP
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|