Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Unable to compile code of pic12f675 in MPLAB

Status
Not open for further replies.

darryl_co

Member level 1
Joined
Feb 23, 2011
Messages
34
Helped
6
Reputation
12
Reaction score
5
Trophy points
1,288
Activity points
1,521
I keep getting errors in MPLAB v8.63 for the code of blinking an led wit PIC12F675. I cannot use the
#use delay
what is wrong with the code please guide
Code:
//#include <12F675.h>
#include <htc.h>
#device ADC=10
#fuses INTRC_IO,NOWDT,NOPUT,NOPROTECT,NOCPD,NOMCLR
#use delay(clock=4000000)
#define GP0 PIN_A0
#define GP1 PIN_A1
#define GP2 PIN_A2
#define GP3 PIN_A3
#define GP4 PIN_A4
#define GP5 PIN_A5
#byte OSCCAL = 0x80

void init()
{
OSCCAL = 0x80; // set internal oscillator to mid frequency
set_tris_a( 0b11111101 ); // set GP1 output, all other inputs
setup_comparator( NC_NC_NC_NC ); // disable comparators
setup_adc_ports( NO_ANALOGS ); // disable analog inputs
setup_adc( ADC_OFF ); // disable A2D
}
main()
{
init();
while ( TRUE ) // blink LED
{
output_high( GP4 ); // turn LED on
delay_ms( 250 ); // wait 250ms
output_low( GP4 ); // turn LED off
delay_ms( 250 ); // wait 250ms
}
}
 

You are using ccs compiler right ?

Code:
#include <htc.h>

is this a custom header file or you added by mistake?

Also where is your processor header file ?

Code:
//#include <12F675.h>

Uncomment this line.
 

You didn't tell which compiler is installed under MPLAB. Apparently you got mixed-up some things.

#use delay is a CCS C specific preprocessor command
htc.h is referring to Hi-Tech C respectively Microchip XC8
 
PWM out

Thanks user FvM.I was using Hi-Tech C compiler.I have modified the code and require help
1. Can I increase the value range of "step" to 1000 instead of 255
2. What is the code for NOP in C
3. How do I add pwm( Change the width) to the output sensing voltage at analog input pin(pot connected) and get the output as in the attached pic
Code:
#include <12F675.h>
#device ADC=10
#fuses INTRC_IO,NOWDT,NOPUT,NOPROTECT,NOCPD,NOMCLR
#use delay(clock=4000000)
#define GP0 PIN_A0
#define GP1 PIN_A1
#define GP2 PIN_A2
#define GP3 PIN_A3
#define GP4 PIN_A4
#define GP5 PIN_A5
#byte OSCCAL = 0x80


void init()
{
OSCCAL = 0x80; 				// set internal oscillator to mid frequency
set_tris_a( 0b11111101 ); 	// set GP1 output, all other inputs
setup_comparator( NC_NC_NC_NC ); // disable comparators
//## How to enable Analog Inputs?
setup_adc_ports( NO_ANALOGS ); // disable analog inputs
setup_adc( ADC_OFF ); 		// enable A2D
}

void main()
{
int step;
init();

	while ( 1 ) // blink LED
	{
// can Value of step be more than 255 say 1000
	for(step=50;step<255;step++) // Loop control
		{	
		output_high( GP4 ); // turn LED on
		output_low( GP2 ); // turn LED on
		delay_us(step); // wait for period(step)
		output_low( GP4 ); // turn LED off
		output_high( GP2 ); // turn LED on
		delay_us(step); // wait for period(step)
		
		}
	}
}
 

Attachments

  • pwm sweep.jpg
    pwm sweep.jpg
    122.2 KB · Views: 138

1. Change 'int step;' to 'unsigned int step;' to double it's maximum value as negative numbers are not needed.
Then change the instruction 'for(step=50;step<255;step++)' to 'for(step=50;step<1000;step++)'. You can use any value up to 65535 and if you want the size of the steps to be bigger, change the third part to 'step += xxx' where xxx is the size you want to increase by each time.

2. I'm not sure of the syntax in HTC but it should be possible to insert an assemble instruction 'nop' in line with the C instructions.

3. Not with this code! That PIC has no internal PWM generator. the nearest you will get is to use the ADC to measure the voltage at the pin then use the result and a derived value in place of the 'step' variable in the loop. If you decide on a maximum value for step of 1000, use the ADC reading in the first delay_us() instruction and 1000-ADC reading in the second delay_us instruction so as one increases the other reduces. That way the total delay (= PWM frequency) should stay fairly constant as the value changes.

Brian.
 
The delay is wrong!!!

You must use the internal clock

Code:
#use delay(internal=4MHz)

This code is for CSS C for pic.

Code:
#include <12F675.h>
#device ADC=10
#fuses INTRC_IO,NOWDT,NOPUT,NOPROTECT,NOCPD,NOMCLR
#use delay(internal=4000000)
#define GP0 PIN_A0
#define GP1 PIN_A1
#define GP2 PIN_A2
#define GP3 PIN_A3
#define GP4 PIN_A4
#define GP5 PIN_A5
#byte OSCCAL = 0x90

void init()
{
   OSCCAL = 0x80;                      // set internal oscillator to mid frequency
   set_tris_a( 0b11111101 );           // set GP1 output, all other inputs
   setup_comparator( NC_NC_NC_NC );    // disable comparators
   //## How to enable Analog Inputs?
   setup_adc_ports( NO_ANALOGS );      // disable analog inputs
   setup_adc( ADC_OFF );               // enable A2D
}

void main()
{
   long int step;
   
   init();

   while ( TRUE )                      // blink LED
   {
      // can Value of step be more than 255 say 1000
      for(step=50;step<1000;step++)    // Loop control
      {   
         output_high( GP4 );           // turn LED on
         output_low( GP2 );            // turn LED on
         delay_us(step);               // wait for period(step)
         output_low( GP4 );            // turn LED off
         output_high( GP2 );           // turn LED on
         delay_us(step);               // wait for period(step)
      }
   }
}
 
Last edited:

Re: PWM out

Thanks user FvM.I was using Hi-Tech C compiler.I have modified the code and require help
1. Can I increase the value range of "step" to 1000 instead of 255
2. What is the code for NOP in C
3. How do I add pwm( Change the width) to the output sensing voltage at analog input pin(pot connected) and get the output as in the attached pic
Code:
#include <12F675.h>
#device ADC=10
#fuses INTRC_IO,NOWDT,NOPUT,NOPROTECT,NOCPD,NOMCLR
#use delay(clock=4000000)
#define GP0 PIN_A0
#define GP1 PIN_A1
#define GP2 PIN_A2
#define GP3 PIN_A3
#define GP4 PIN_A4
#define GP5 PIN_A5
#byte OSCCAL = 0x80


void init()
{
OSCCAL = 0x80; 				// set internal oscillator to mid frequency
set_tris_a( 0b11111101 ); 	// set GP1 output, all other inputs
setup_comparator( NC_NC_NC_NC ); // disable comparators
//## How to enable Analog Inputs?
setup_adc_ports( NO_ANALOGS ); // disable analog inputs
setup_adc( ADC_OFF ); 		// enable A2D
}

void main()
{
int step;
init();

	while ( 1 ) // blink LED
	{
// can Value of step be more than 255 say 1000
	for(step=50;step<255;step++) // Loop control
		{	
		output_high( GP4 ); // turn LED on
		output_low( GP2 ); // turn LED on
		delay_us(step); // wait for period(step)
		output_low( GP4 ); // turn LED off
		output_high( GP2 ); // turn LED on
		delay_us(step); // wait for period(step)
		
		}
	}
}

the above code is 100% CCS. You either install CCS or change your code.
 

I am using CCS C compiler so while compiling the code now I do not get errors, thanks to user FvM
Is it possible to read the value of the variable "step" in debugging mode similar to print in vb?

- - - Updated - - -

what is the range of read_adc(); is it 0 to 255? can we set the range according to our choice?
 

I believe it's all in the CCS documentation. #device ADC=10 sets the ADC range to 10 bit, 0 - 1023.
 

I found that variable can be read with a printf command but I cannot get it right. Please provide me the code to read the variable "step"
 

Step variable in the code I have posted. In ASM language there is a code start 0x00 isn't it required in C language
 

How do I use the MCLR as reset. Connecting a resistor of 10k from the pin and than shorting MCLR pin to ground does not do anything

- - - Updated - - -

Had to define as int16, only unsigned int didn't help

- - - Updated - - -

Had to define as int16, only unsigned int didn't help
 

Remove NOMCLR from the configuration fuses line. You are telling it to ignore the MCLR pin and connect it internally to VDD.

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top