Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

12F675 Ext Oscillator Won't Work?

Status
Not open for further replies.

wuchy143

Member
Hi All,

I'm trying to get a 4Mhz external oscillator to work on a piece of code I have already got to work with the internal oscillator. I"m using C and the HIgh Tech C Compiler lite. Any thoughts on what else I need to do in my config work other than selecting XT? Code below.

Code:
#include <htc.h>

#define _XTAL_FREQ  4000000                 // oscillator frequency for delay

__CONFIG(MCLREN & UNPROTECT & BORDIS & WDTDIS & PWRTEN & INTIO & XT);

void CountTime(void);					   //Function responsible for doing the actual
										   //counting of time
void send_8bit_serial_data(int data);
void TurnOnLED(void);                      //Function to turn on LED once an hour has elapsed
void time_to_send_data(void);              //Function to check if it's time to send data to LCD
void WriteToEEprom(int min);               //Function to write data to EEPROM at address 0x01
void ReadFromEEprom(void);                 //Function to write data to EEPROM at address 0x01
void HexToBCD(int min);                   //Function to Convert to BCD and Transmit result to LCD

unsigned int c = 0;                        //c keeps track of how many times TMRO overflows
unsigned int hour = 0;                     //16 bit variable hour is time in hours
unsigned int min = 0;                      //16 bit variable min is time in minutes 
unsigned int data = 0;                     //buffer for RS232 data to be sent to LCD
unsigned int c2 = 0;                       //c2 keeps track of how many times TMR1 overflows                       


void main()
{
OPTION = 0b00000111;                       //Assigning Prescalar
          //----111-                         prescaler assigned to timer0 (PSA = 1)
                                           //prescale = 256 (PS = 111)
                                           //-> timeout = 255us
ei();					                   //all interrupts enabled(macro from High Tech C)
T0IE = 1;                                  //enable Timer0 interrupt
CMCON = 0x07;                              // Digital I/O
ANSEL = 0;                                 // Digital I/O
TRISIO = 0b00001000;                       // Set Pin I/O
STATUS = 0x00;
T1CON =0b10000101;						   //setting up Timer1

ReadFromEEprom();                          //Checking to see if this is the first powerup of chip
if(EEDATA == 0xFF){                         //if it's the first power up initialize min to 0
min = 0;
EEDATA = 0;
}
else{                                      //if it isn't the first powerup set min = to eprom data
ReadFromEEprom();
min = EEDATA;	
}  


//***************************************************************************//
//****************************   MAIN LOOP   ********************************//
//***************************************************************************//
for(;;){
	       
	
CountTime();
time_to_send_data();
       }

}

//*********************  INTERRUPT SERVICE ROUTINE #1  *********************//
//Interrupt goes off whenever Timer0 overflows which is every 256uS ********//          
//given how the prescalar is set up above in OPTION ************************//                         
void interrupt Timer0(void)
{

//	if(c > 916)                           //bootstrap and suspenders so c doesn't overflow past 923
//	c = 0;


	while(T0IF == 1){
	T0IF = 0;                             //clear Timer0 interrupt overflow bit
    c = c + 1;                            //increase c to show Timer0 has overflowed
 }
	
	while(EEIF == 1)                      //eeprom write finished bit
	EEIF = 0;                             //clear it for next write

}

//***********************  COUNTING TIME FUNCTION  ************************//

void CountTime(void)
{
	while(c == 916){ 
				c = 0;                   //c ranges from 0-59 to simulate seconds
				min = min + 1;           //it clears seconds to start counting again
				HexToBCD(min);
			
                WriteToEEprom(min);      //update eeprom with min value
			   
			
	        }
			
	while(min == 60){                    //min value ranges from 0-59 to simulate minutes
			    hour = hour + 1;         //it clears minutes to start counting again
			    min = 0;
					    }
}

/************    TURN ON LED FUNCTION     **********************/
void TurnOnLED(void){

CMCON = 0x07;                            // Digital I/O
ANSEL = 0;                               // Digital I/O
TRISIO = 0b00001000;                     // Set Pin I/O
STATUS = 0x00;
GPIO = 1;                                //Turn LED on
for(;;) {                                //keep it on forever
    ;
   }
 }


/***********    SERIAL TRANSMITTER FUNCTION   ********************/
void send_8bit_serial_data(int data)
{
   unsigned char i;
   GPIO = 0;						     //start bit from high to low
   _delay(390);                          //delay to sync loop	
   
   for(i = 0; i < 8; i++)
   {
       if (data & 0x01){			    // consider rightmost bit
          GPIO = 1;                     // set line high if bit is 1, low if bit is 0
		  
	}
       else
          GPIO = 0;
          _delay(390);                  //delay to sync loop
 		  data >>= 1;                   // shift byte left so next bit will be leftmost
   }
 
    GPIO = 1;						    //stop bit
    _delay(400);                        //delay to sync loop
   
}


/*********  CHECKING IF TIME TO TRANSMIT FUNCTION  **************/
void time_to_send_data(void)
{
while(TMR1IF == 1){                      //checking overflow bit on Timer1
	TMR1IF = 0;                          //clearing overflow bit
	c2 = c2 + 1;                         //increment counter(c2)
}

while(c2 == 100){                          //change c2 = 10,000 for about 10 mins
    c2 = 0;                              //clear c2 to continue counting again
	data = min;                          //shove minute into data buffer

	
	//HexToBCD(hour);						 //converts to BCD and trasmits to LCD
	    }
}

/************    WRITING TO THE EEPROM    **********************/

void WriteToEEprom(min)
{
EEADR =  0b00000001;                       //location where data goes in memory
EEDATA = min;                              //set data into eeprom data reg.
WREN = 1;                                  //enable write cycle
di();									   //disable all interrupts
EECON2 = 0x55;						       //unlock write(aling with this line
EECON2 = 0xAA;							   //the following 2 lines are required
WR = 1;	                                   //to start write seq. WR starts write
ei();                                      //enable all interrupts
WREN = 0;                                  //disable write cycle
}

/************    READING FROM THE EEPROM    **********************/
void ReadFromEEprom(void)
{
RP0 = 1;                                   //register bank select bit(bank 1)
EEADR = 0b00000001;                        //setting the address to be accessed in EEPROM
RD = 1;                                    //Enabling/Performing a read to EEADR
min = EEDATA;                              //loading whats in EEPROM to min

}

/************* LCD Stuff   ***********************/



void HexToBCD(min){
unsigned int division = 0;
unsigned int min2 = 0;
unsigned int division2 = 0;

	GPIO = 1;
	//_delay(100);
	data = 0xFE;                         //clear screen
	send_8bit_serial_data(data);
	data = 0x51;
	send_8bit_serial_data(data);

	
	data = 0xFE;
	send_8bit_serial_data(data);
	data = 0x46;
	send_8bit_serial_data(data);         //set curson home


min2 = min;

division = min2 / 10000;
division = division + 0x30;
data = division;
send_8bit_serial_data(data);


division = min2 % 10000;
division = division / 1000;
division = division + 0x30;
data = division;
send_8bit_serial_data(data);


division = min2 % 1000;
division = division / 100;
division = division + 0x30;
data = division;
send_8bit_serial_data(data);


division = min2 % 100;
division = division / 10;
division = division + 0x30;
data = division;
send_8bit_serial_data(data);


division = min2 % 10;
division = division / 1;
division = division + 0x30;
data = division;
send_8bit_serial_data(data);


data = 0xFE;                          //move curson one space to right
	send_8bit_serial_data(data);
    data = 0x4A;                          
	send_8bit_serial_data(data);	
	
	data = 0x4D;							//put a M to LCD
	send_8bit_serial_data(data);
	
	data = 0x69;                            //put a i to LCD
	send_8bit_serial_data(data);
	
	data = 0x6E;                            //put a n to LCD
	send_8bit_serial_data(data);
//	data = 0x72;							//put a u to LCD
//	send_8bit_serial_data(data);
//
//	data = 0x73;                            //put a t to LCD
//	send_8bit_serial_data(data);
//

}
 
Hi,

27 pf. Though, I made an interesting discovery.

I"m using PIcKit2 to program my PIC12F675. So I clicked on the configuration word in the PIckit 2 Programmer GUI and saw that my _config bits are not getting set right. So, I changed them in the GUI. Loaded the hex into chip and waaallaaaa. It now works with the external crystal. I wonder why I have to do this manually. I must be doing something wrong in my config? Looks like I need to look at the High Tech C manual :)

-mike
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top