PIC16f690, initialisation of ports, LEDS,reading from a pin

Thread Starter

fantabulous68

Joined Nov 3, 2007
51
PIC16f690, initialisation of ports, LEDS,reading from a pin

I want an LED connected to pin RAO to glow if pin RA1==0

when i run my code the LED keeps pulsing on and off regardless of what value RA1 equals to.

could someone plz help me. Thanks

Rich (BB code):
#include <pic.h>
#include "pic.h"
#include "delay.h"
#include "math.h"
#include <stdio.h>
#include <stdlib.h>

void init(void)
{  
	TRISB=0x00; 
	TRISC=0xFC;
	ANSEL=0;        // Set inputs to digital
       TRISA=0x02;	//making pin RA1 input
}

void main(void)
{
    init();	// initialise I/O ports, LCD
    while(1)                        //  Loop forever
  {

    if(RA1==0)          		//if receiver detects something then output is active low
      		{PORTA=0x01;}	//light up led connected to RA0 if detect object
    else 
      		{PORTA=0x00;}	//dont light led if no detection
  } 
}
 

rjenkins

Joined Nov 6, 2005
1,013
I can't see anything obviously wrong, but I am half asleep, sorry.

Do you have the chip config set correctly for the oscillator type, and is the watchdog disabled?

You should be able to turn on the register view and run it in MPLab's built-in simulator to see what is happening step-by-step.
 

Thread Starter

fantabulous68

Joined Nov 3, 2007
51
After I disabled the watchdog timer, it fixed an LCD displaying error that i was getting. Thank you so much. I didnt know that i supposed to disable it. I am a beginner. im using a PIC 16f690.

Do you have the chip config set correctly for the oscillator type
what do u mean. This is all the code i have written relating to the oscillator. What bits do i need to set???

Rich (BB code):
/////////////////////////////DELAY///////////////////////////////
void DelayMs(unsigned char cnt)
{
#if	XTAL_FREQ <= 2MHZ
	do {
		DelayUs(996);
	} while(--cnt);
#endif

#if    XTAL_FREQ > 2MHZ	
	unsigned char	p;
	do {
		p = 4;
		do { 
			DelayUs(250);
		} while(--p);
	} while(--cnt);
#endif
}

void DelayS(unsigned char cnt)
{
	for (j=0; j<(cnt*10); j++)
		DelayMs(100);
}
///////////////////////////DELAY END/////////////////////////////

void init(void)
{ 
	OSCCON|=0x60; //set fosc to 4Mhz
	TRISB=0x00; 
	TRISC=0xFC;
	ANSEL=0;	// 
	TRISA=0x02;	//
    //ANSELH=0x00;
	lcd_init(); //call LCD initialisation
	GIE=0; //disable global interrupt
}
it used to display infrared liquid level detecto
now it displays infrared liquid level detector.
 

Thread Starter

fantabulous68

Joined Nov 3, 2007
51
Did you pullup with a 10k resistor pin RA1 (portA.1) ?
i have an infrared receiver module which outputs a low when it detects an object. The receiver is is connected to RA1. Ill be using switches later in my design, thanks for telling me about the pull ups.
 

Thread Starter

fantabulous68

Joined Nov 3, 2007
51
My design is an infrared liquid level detector. Im in the beginning stages of my design. I want to get the led indicator working for now. The hardware for my proximity detection circuit is working. When an object is detected the receiver IC outputs a low which is fed INTO pin RA1. i want an LED connected to RA0 to glow when detection occurs. The LCD functions all work.

Im having some problems with my code. I suspect the problem lies in my init function and main program. tink my initialisation of portA is wrong and maybe the order in which i call ledindicator function.


1) The LEDindicator function doesnt work when i join it as a entire program with the LCD funtion. It only works separately. I need it to work together.

Please assist me in getting the code working. Thanx. The LED is always on and i dont want that.

i only want the led to glow when an object is detected. eg if detect object then LED glows, if dont detect then led doesnt glow. i dont want the led permanently on. What should i do?


Rich (BB code):
#include <pic.h>
#include "pic.h"
#include "delay.h"
#include "math.h"
#include <stdio.h>
#include <stdlib.h>
 
void FloatToStr(float , char[]);
void DelayMs(unsigned char);
void lcd_cmd(unsigned char);
void lcd_data(unsigned char); 
void lcd_clear(void);
void lcd_puts(const char[]);
void lcd_goto_L1(void);
void lcd_goto_L2(void);
void lcd_cursor(unsigned char);
void lcd_init(void);
void enable_interrupt(void);
void interrupt isr(void);
void calc_distance(void);
void init(void);
void read_input_sw(void);
void home_screen(void);
void _40KhzTransmit(void); 
void IndicatorLED(void);

#define LCD_RS RC0		//LCD RS pin
#define LCD_EN RC1		//LCD EN pin
#define LCD_STROBE()	LCD_EN = 1; asm("nop"); asm("nop"); LCD_EN = 0

char input_sw;
unsigned char i,j,k;
char temp[8];

///////////////////////CONVERT FLOAT TO STRING///////////////////
// This function was taken from the CAVR library. It was modified slightly
// to suit our design.
void FloatToStr(float n, char str[])
{
float scale;
unsigned char d,f;
f=0;i=0;
if (n<0.0) {n=-n; str[f]='-'; f++;};
n=n+0.005;
scale=1.0;
while (n>=scale) {scale=scale*10.0; ++i;};
if (i==0) {str[f]='0'; f++;}
else
while (i--)
	  {
      scale=floor(0.5+scale/10.0);
      d=(unsigned char) (n/scale);
      str[f]=d+'0';
      n=n-scale*d;
	  f++;
      };

str[f]='.';
f++;
for (j=0;j<=1;j++) //2 decimal points
      {
      n=n*10.0;
      d=(unsigned char) n;
      str[f]=d+'0';
      n=n-d;
	  f++;
	  };
str[f]='\0';
}
///////////////////END CONVERT FLOAT TO STRING///////////////////

/////////////////////////////DELAY///////////////////////////////
void DelayMs(unsigned char cnt)
{
#if	XTAL_FREQ <= 2MHZ
	do {
		DelayUs(996);
	} while(--cnt);
#endif

#if    XTAL_FREQ > 2MHZ	
	unsigned char	p;
	do {
		p = 4;
		do { 
			DelayUs(250);
		} while(--p);
	} while(--cnt);
#endif
}

void DelayS(unsigned char cnt)
{
	for (j=0; j<(cnt*10); j++)
		DelayMs(100);
}
///////////////////////////DELAY END/////////////////////////////

//////////////////////////////LCD SETUP//////////////////////////
/* send a command to the LCD */
void lcd_cmd(unsigned char c)
{
	DelayMs(20); //wait for LCD to be ready
	LCD_RS = 0;	 //write instruction
	PORTB = (c & 0xF0); //load upper nibble on LCD data lines
	LCD_STROBE(); //send instruction to LCD
	PORTB = ((c << 4) & 0xF0); //load upper nibble on LCD data lines
	LCD_STROBE(); //send instruction to LCD   	
}

/* send data to the LCD */
void lcd_data(unsigned char c)
{
	DelayMs(20); //wait for LCD to be ready
	PORTB = 0x00;
	LCD_RS = 1; //write data
    PORTB |= (c & 0xF0); //load upper nibble on LCD data lines     
	LCD_STROBE(); //send instruction to LCD
	PORTB &= 0x00; //load upper nibble on LCD data lines
	PORTB |= ( (c << 4) & 0xF0); 
	LCD_STROBE(); //send instruction to LCD
}

/*Clear the LCD*/
void lcd_clear(void)
{
	lcd_cmd(0x01); //command to clear LCD
}

/*write a string of chars to the LCD*/
void lcd_puts(const char s[])
{
	j = -1;
	while(s[++j]!=('\0')) // send characters until null character reached
		lcd_data(s[j]);
}

/*go to beginning of line 1*/
void lcd_goto_L1(void)
{
	lcd_cmd(0b10000000); // command to go to line 1
}

/*go to beginning of line 2*/
void lcd_goto_L2(void)
{
	lcd_cmd(0b11000000); // command to go to line 2
}

/*move cursor "x" positions to the right*/
void lcd_cursor(unsigned char x)
{
	lcd_cmd(((x)&0x7F)|0x80); 
}

/*initialise the LCD - put into 4 bit mode*/
void lcd_init(void)
{
	LCD_RS = 0;	
	LCD_EN = 0;
	DelayMs(20); //wait for LCD startup
	lcd_cmd(0x28);	// 4-bit mode
	lcd_cmd(0x08);	// display off
	lcd_cmd(0x01);	// clear display
	lcd_cmd(0x0C);	// disp. on, cursor off, cursor blink off
	lcd_cmd(0x06);	// entry mode
	lcd_cmd(0x80);  // initialise DDRAM address to zero
}
//////////////////////////LCD SETUP END//////////////////////////

void init(void)
{ 
	OSCCON|=0x60; //set fosc to 4Mhz
	TRISB=0x00; 
	TRISC=0xFC;
	ANSEL=0;	// 
	TRISA=0x02;	//
    //ANSELH=0x00;
	lcd_init(); //call LCD initialisation
	GIE=0; //disable global interrupt
}
 
  
void home_screen(void)
{
	lcd_clear();
	lcd_goto_L1();
	lcd_puts("INFRARED LIQUID"); //home screen message (line 1)
	lcd_goto_L2();
	lcd_puts("LEVEL DETECTOR"); //home screen message (line 2)

}

void _40KhzTransmit(void)
{	
	int t=0;
	int cnt=0;
	while(cnt<20)
		{	if (t==0)
			{
				PORTA=PORTA^0x04;  //toggles RA2
				DelayUs(13);
				cnt++;
				t=1;
			}
			else if (t==1)
			{	
				
				PORTA=PORTA^0x04;
				DelayUs(12);
				cnt++;
				t=0;
			}
		}
}

void IndicatorLED(void)
{ 


    while(1)                        //  Loop forever
  {


    if(RA1==0)          	//if receiver IC TSOP48 at pin RA1 detects something then output is active low
      		{PORTA=0x01;}	//light up led connected to RA0 if detect object
    else 
      		{PORTA=0x00;}	//dont light led if no detection

  } 
}

void main(void)
{
	init();	// initialise I/O ports, LCD
	while (1)
	IndicatorLED();
	home_screen();
	
}
 
Last edited:

Thread Starter

fantabulous68

Joined Nov 3, 2007
51
i got it to do what i wanted. It just had to swap the order of the two functions in my main program. YAY :). Im a happy girl now

im going to start working on my timer 1 code. i need the time interval between the reflected and detected infrared light in order to calculate distance from object. :)
 
Top