Q 16F690 and need of x-tal

Thread Starter

FroceMaster

Joined Jan 28, 2012
702
Have this setup,
16F690 2*16LCD., connected data 4-7 on RB4-7
RC0 RS, RC1 enable

Do i need x-tal on PIC to get the party going ?,
Is there not an internal x-tal,
Or is it another problem,

Here is Main and LCD.c
Code:
#include <htc.h>
#include <stdio.h>
#include <stdlib.h>
#include "lcd.h"
#include <string.h>

// CONFIG
#pragma config FOSC = INTRCIO   // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = ON        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON       // MCLR Pin Function Select bit (MCLR pin function is MCLR)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON       // Brown-out Reset Selection bits (BOR enabled)
#pragma config IESO = ON        // Internal External Switchover bit (Internal External Switchover mode is enabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
static void interrupt isr(void)   // Here is interrupt function - the name is unimportant.
{

}

void setup(void)
{
    IRCF0=1; // 8 mhz
    IRCF1=1;
    IRCF2=1;
    SCS=0; // internel clock
   
    TRISA0=1; //set all A port in
TRISA0=1;
TRISA1=1;
TRISA2=1;
TRISA3=1;
TRISA4=1;
TRISA5=1;



TRISB4=0; // = out
TRISB5=0;
TRISB6=0;
TRISB7=0;

TRISC0=0; // Out
TRISC1=0;
TRISC2=1; // Other in;
TRISC3=1;
TRISC4=1;
TRISC5=1;
TRISC6=1;
TRISC7=1;
ANSEL=0; // all digital.

}


void main(void) {
    setup();
    lcd_init();
    lcd_goto(0);
    lcd_puts("Hello");
   
    while (1)
    {
    }
   
   
  
}
And lcd.c
Code:
#ifndef _XTAL_FREQ
// Unless specified elsewhere, 8MHz system frequency is assumed
#define _XTAL_FREQ 8000000
#endif


#include    <htc.h>
#include    "lcd.h"

#define    LCD_RS RC0
#define LCD_EN RC1
#define LCD_D4 RB4    // Data bits
#define LCD_D5 RB5    // Data bits
#define LCD_D6 RB6    // Data bits
#define LCD_D7 RB7    // Data bits

#define    LCD_STROBE()    ((LCD_EN = 1),(LCD_EN=0))

/* write a byte to the LCD in 4 bit mode */

void
lcd_write(unsigned char c)
{   static unsigned char temp;
     temp=c;
    __delay_us(320);
   
    if(c & 0x80) LCD_D7=1; else LCD_D7=0;
    if(c & 0x40) LCD_D6=1; else LCD_D6=0;
    if(c & 0x20) LCD_D5=1; else LCD_D5=0;
    if(c & 0x10) LCD_D4=1; else LCD_D4=0;
    LCD_STROBE();
    if(c & 0x08) LCD_D7=1; else LCD_D7=0;
    if(c & 0x04) LCD_D6=1; else LCD_D6=0;
    if(c & 0x02) LCD_D5=1; else LCD_D5=0;
    if(c & 0x01) LCD_D4=1; else LCD_D4=0;

    LCD_STROBE();
}

/*
*     Clear and home the LCD
*/

void
lcd_clear(void)
{
    LCD_RS = 0;
    lcd_write(0x1);
    __delay_ms(16);
}

/* write a string of chars to the LCD */

void
lcd_puts(const char * s)
{
    LCD_RS = 1;    // write characters
    while(*s)
        lcd_write(*s++);
}

/* write one character to the LCD */

void
lcd_putch(char c)
{
    LCD_RS = 1;    // write characters
    lcd_write( c );
}


/*
* Go to the specified position
*/

void
lcd_goto(unsigned char pos)
{
    LCD_RS = 0;
    lcd_write(0x80+pos);
}
   
/* initialise the LCD - put into 4 bit mode */
void
lcd_init()
{
       
//NY RUTINE       
        LCD_RS = 0;    // write control bytes

__delay_ms(280);// power on delay

    LCD_D4 = 1;    // init!   
    LCD_D5 = 1; //
    LCD_STROBE();   
    __delay_ms(40);

    LCD_STROBE();    // init!   
    __delay_us(800);

    LCD_STROBE();    // init!   
    __delay_us(40);

    LCD_D4 = 0;    // set 4 bit mode
    LCD_STROBE();   
    __delay_us(320);
   
    lcd_write(0x28);// 4 bit mode, 1/16 duty, 5x8 font, 2lines
    lcd_write(0x0C);// display on
    lcd_write(0x06);// entry mode advance cursor
    lcd_write(0x01);// clear display and reset cursor

}
This lcd. code works fine in another project with other MCU,
Other project i have done with 16F690 has allways been with external x-tal.

What to do ?

I only get first line of LCD filled with squares.
 

Ian Rogers

Joined Dec 12, 2012
1,136
Other project i have done with 16F690 has allways been with external x-tal.
What crystal did you use???

Two things.... Is there a pullup on MCLR as you have set it to external..
The Lcd_write has no housekeeping delay... With a slower crystal, it may not have been so prevalent!!

I always use a 1ms delay on exiting the lcd write routines ( unless I read the busy flag! )
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
702
Hi Ericgibbs , i dont think it matter.
If i control FOSC in CONFIG then it doesnt matter,
upload_2018-11-24_23-28-13.png
Damm,read wrong, i try again with =1, but still the same,

Ian,, for that old project i used 32,768 khz.
MCLR is changed to OFF, still the same,

Delay's in LCD, will try it
Edit, have tried also with no luck.
 

spinnaker

Joined Oct 29, 2009
7,830
Hi Ericgibbs , i dont think it matter.
If i control FOSC in CONFIG then it doesnt matter,
View attachment 164379
Damm,read wrong, i try again with =1, but still the same,

Ian,, for that old project i used 32,768 khz.
MCLR is changed to OFF, still the same,

Delay's in LCD, will try it
Edit, have tried also with no luck.

You never told us what your problem is. Is the program running but thge LCD is not working? Or is the code not running at all?
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
702
Hi,
Code is running, but not as it should i think,'
Have edited my files, here are they,
Code:
#include <htc.h>
#include <stdio.h>
#include <stdlib.h>
#include "lcd.h"
#include <string.h>

// CONFIG
#pragma config FOSC = INTRCIO   // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = ON        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF      // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON       // Brown-out Reset Selection bits (BOR enabled)
#pragma config IESO = OFF       // Internal External Switchover bit (Internal External Switchover mode is disabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)

#ifndef _XTAL_FREQ
// Unless specified elsewhere, 8MHz system frequency is assumed
#define _XTAL_FREQ 8000000
#endif

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
static void interrupt isr(void)   // Here is interrupt function - the name is unimportant.
{

}

void setup(void)
{
    IRCF0=1; // 8 mhz
    IRCF1=1;
    IRCF2=1;
    SCS=1; // internel clock
    OSTS=0;
    TRISA0=1; //set all A port in
 
   
TRISA0=1;
TRISA1=1;
TRISA2=1;
TRISA3=1;
TRISA4=1;
TRISA5=1;



TRISB4=0; // = out
TRISB5=0;
TRISB6=0;
TRISB7=0;

TRISC0=0; // Out
TRISC1=0;
TRISC2=1; // Other in;
TRISC3=1;
TRISC4=1;
TRISC5=1;
TRISC6=1;
TRISC7=0; // rc7 out
ANSEL=0; // all digital.

}


void main(void) {
    setup();
    lcd_init();
    lcd_goto(0);
    lcd_puts("Hello");
   
    while (1)
    {
        RC7=!RC7;   // Led conneted to RC7, should blink steady on off.
                __delay_ms(500);
       
    }
   
   
  
}
And the LCD.C
Code:
#ifndef _XTAL_FREQ
// Unless specified elsewhere, 8MHz system frequency is assumed
#define _XTAL_FREQ 8000000
#endif


#include    <htc.h>
#include    "lcd.h"

#define    LCD_RS RC0
#define LCD_EN RC1
#define LCD_D4 RB4    // Data bits
#define LCD_D5 RB5    // Data bits
#define LCD_D6 RB6    // Data bits
#define LCD_D7 RB7    // Data bits

#define    LCD_STROBE()    ((LCD_EN = 1),(LCD_EN=0))

/* write a byte to the LCD in 4 bit mode */

void
lcd_write(unsigned char c)
{   static unsigned char temp;
     temp=c;
    __delay_us(320);
   
    if(c & 0x80) LCD_D7=1; else LCD_D7=0;
    if(c & 0x40) LCD_D6=1; else LCD_D6=0;
    if(c & 0x20) LCD_D5=1; else LCD_D5=0;
    if(c & 0x10) LCD_D4=1; else LCD_D4=0;
    LCD_STROBE();
    if(c & 0x08) LCD_D7=1; else LCD_D7=0;
    if(c & 0x04) LCD_D6=1; else LCD_D6=0;
    if(c & 0x02) LCD_D5=1; else LCD_D5=0;
    if(c & 0x01) LCD_D4=1; else LCD_D4=0;

    LCD_STROBE();
}

/*
*     Clear and home the LCD
*/

void
lcd_clear(void)
{
    LCD_RS = 0;
    lcd_write(0x1);
    __delay_ms(16);
}

/* write a string of chars to the LCD */

void
lcd_puts(const char * s)
{
    LCD_RS = 1;    // write characters
    while(*s)
        lcd_write(*s++);
}

/* write one character to the LCD */

void
lcd_putch(char c)
{
    LCD_RS = 1;    // write characters
    lcd_write( c );
}


/*
* Go to the specified position
*/

void
lcd_goto(unsigned char pos)
{
    LCD_RS = 0;
    lcd_write(0x80+pos);
}
   
/* initialise the LCD - put into 4 bit mode */
void
lcd_init()
{
       
//NY RUTINE       
        LCD_RS = 0;    // write control bytes

__delay_ms(280);// power on delay

    LCD_D4 = 1;    // init!   
    LCD_D5 = 1; //
    LCD_STROBE();   
    __delay_ms(40);

    LCD_STROBE();    // init!   
    __delay_us(800);

    LCD_STROBE();    // init!   
    __delay_us(40);

    LCD_D4 = 0;    // set 4 bit mode
    LCD_STROBE();   
    __delay_us(320);
   
    lcd_write(0x28);// 4 bit mode, 1/16 duty, 5x8 font, 2lines
    __delay_ms(1);
    lcd_write(0x0C);// display on
    __delay_ms(1);
    lcd_write(0x06);// entry mode advance cursor
    __delay_ms(1);
    lcd_write(0x01);// clear display and reset cursor
__delay_ms(1);
}
Searched a littel for the LCD should be anything other than std, but seem to be right,
Here is link to LCD. https://dk.farnell.com/midas/mc2160.../Element14_Denmark/search#anchorTechnicalDOCS

Led is flashing in code, but not correct, video show
https://www.dropbox.com/s/9bglwawix4131aq/20181125_105407.mp4?dl=0
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
702
Ok, should not be nesarysarry, but the video shows,
Led on for 2 sec, then off for 0.5 sec

Code should give a steady blink on off at 500 ms.
 

ericgibbs

Joined Jan 29, 2010
18,865
hi FM,
I don't program in C, Arduino is similar.
But I would suggest, if yo are trying to check the xtal rate, expand this piece of Code.
So that RC7 =0 and RC7=1 are each assigned a 500ms Delay period, in that While Loop.

while (1)
{
RC7=!RC7; // Led conneted to RC7, should blink steady on off.
__delay_ms(500);

}
 

ericgibbs

Joined Jan 29, 2010
18,865
OK,
I expect one of our resident 'C' experts will now be able to spot the problem.

I have used the 16F690 with code written using Assembler, never had this sort of problem, perhaps one day I will learn C.
The Ardunio version of 'C' is pretty simple.
E
 

spinnaker

Joined Oct 29, 2009
7,830
Only two things can go wrong (assuming the LCD code is working and the LCD itself works). Clock and wiring. Delays are very important with LCDs so you need to make certain you are delaying the length of time you think you are dealying.

1. Verify that you clock macro is correct to what you clock actually is.

2. Verify your connections. Write a small program to toggle each bit one at a time and verify each bit is wired to the LCD as designed.

But I think we have been doen this road with LCDs with ou once before. Haven't we?
 
Top