Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
LinkBack Thread Tools Display Modes
Old 24th February 2007, 02:35 PM   (permalink)
Default Beginner to PIC microships

Hi,

I've bought the PICkit 1 and are now trying to programme the PIC12F675. Im using HI-TECH (HI-TIDE?) C to write to code and programme it to the chip with "PICkit 1 Classic".

I want four of the pins on the chip to be input and two of them to be output, this should be possible right?

To do this i've used: TRISIO = 0b11001111;
But what should I then set GPIO to set the two outputs high/low?

Thanks in advance!
Mikkel

*microchips!

Last edited by mikkelbg; 24th February 2007 at 07:44 PM.
mikkelbg is offline  
Old 24th February 2007, 08:22 PM   (permalink)
Default

Quote:
But what should I then set GPIO to set the two outputs high/low?
In assembly:
bsf GPIO, GP4 ;To make bit 4 of GPIO high
bsf GPIO, GP5 ;To make bit 5 of GPIO high
bcf GPIO, GP4 ;To make bit 4 of GPIO low
bcf GPIO, GP5 ;To make bit 5 of GPIO low

In C this should work(Not sure on HighTech C):
GP4 = 1; //To make bit 4 of GPIO high
GP5 = 1; //To make bit 5 of GPIO high
GP4 = 0; //To make bit 4 of GPIO low
GP5 = 0; //To make bit 5 of GPIO low
__________________
--- The days of the digital watch are numbered. ---
kchriste is offline  
Old 25th February 2007, 09:54 AM   (permalink)
Default

That gives me the following error: undefined identifier "GP4".

By reading posts on this site i understand that I somehow has to turn stuff like analog ports off, is that correct?

Here's all the code in C (found it in the examples that came with the PICkit 1):
Code:
GPIO = 0;		//Clear GPIO
ANSEL = 0b00000000;	// configure A/D inputs as digital I/O
TRISIO = 0b11001111;	//GPIO 3,4 outputs, GPIO 0,1,2,5,6,7 inputs
OPTION = 0b11010000;	// Timer0 internal clock, 1:2 prescale
CMCON = 0b00000000;	// configure comparator inputs as digital I/O
GP4 = 1;		//To make bit 4 of GPIO high
mikkelbg is offline  
Old 25th February 2007, 05:18 PM   (permalink)
Default

Quote:
That gives me the following error: undefined identifier "GP4".
By reading posts on this site i understand that I somehow has to turn stuff like analog ports off, is that correct?
That is correct, but it is not the source of the undefined identifier error. Either you haven't included the right C header file for PIC12F675 or HighTech-C uses a slightly different identifier. I don't know for sure because I've only used Microchips C with their 18F series chips.... But you need to change this:
CMCON = 0b00000000; // configure one comparator input as digital I/O
to this:
CMCON = 0b00000111; // configure all comparator inputs as digital I/O
So that GP0 & 1 are IO only....
__________________
--- The days of the digital watch are numbered. ---
kchriste is offline  
Old 28th February 2007, 03:39 PM   (permalink)
Default Ports dosn't react as expected

Looked in some of the header files and found no GP4, but did find GPIO(0-5), that might be it?

It still dosn't work though. It almost seems like it's random (not only the two outputs but all the ports on the chip) which ports opens when changing the GPIO4 and GPIO5 to either 0 or 1.

Here is the full sourcecode:

main.c
Code:
#include <htc.h>
#include "main.h"

void
main(void)
{
	Init();	//Initalize the chip
	while (1){
		GPIO4 = 1; //To make bit 4 of GPIO high/low
		GPIO5 = 1; //To make bit 5 of GPIO high/low
	}
}

void Init(void)
{
	#asm			
		call 0x3FF	    //Load Factory Calibration Value Into OSCCAL
				
		bsf _STATUS,5  //BANK1
		movwf _OSCCAL
	#endasm

	ANSEL = 0b00000000;	// configure A/D inputs as digital I/O
	TRISIO = 0b11001111; //GPIO 3,4 outputs, GPIO 0,1,2,5,6,7 inputs
	OPTION = 0b11010000;	// Timer0 internal clock, 1:2 prescale
	
	CMCON = 0b00000111; // configure all comparator inputs as digital I/O
}
main.h
Code:
#ifndef MAIN_H_
#define MAIN_H_

#include <pic.h>

__CONFIG(UNPROTECT & BOREN & MCLRDIS & PWRTEN & WDTDIS & INTIO);   

void Init(void);

#endif /*MAIN_H_*/
Thanks for your help so far!
Mikkel
mikkelbg is offline  
Old 28th February 2007, 03:56 PM   (permalink)
Default

You may try this:

Code:
#include <htc.h>
#include "main.h"

void main(void)
{
	Init();	//Initalize the chip
	while (1){
		GPIO0 = 1; //To make bit 0 of GPIO high/low
		GPIO1 = 1; //To make bit 1 of GPIO high/low
	}

}

void Init(void)
{
	#asm	
		
        bsf STATUS, RP0   ;Bank 1
        call 3FFh         ;Get the cal value
        movwf OSCCAL      ;Calibrate
        bcf STATUS, RP0   ;Bank 0 

	#endasm

	CMCON = 0b00000111; // configure all comparator inputs as digital I/O
	ANSEL = 0b00000000;	// configure A/D inputs as digital I/O
	TRISIO = 0b111100;       //GPIO0-1 outputs GPIO2-3-4-5 inputs 

	OPTION = 0b11011000;	
		
}
GPIO3 is input only
eng1 is offline  
Old 28th February 2007, 05:28 PM   (permalink)
Default Gpio3?

I didn't use GPIO3? Shouldn't it be possible to use GPIO4 and 5 as outputs?

EDIT: Oh sorry, did - will try it out
mikkelbg is offline  
Old 28th February 2007, 05:33 PM   (permalink)
Default

You can use GPIO4-5 as outputs

EDIT: you didn't use GPIO3, but the comment after TRISIO confused me: TRISIO = 0b11001111; //GPIO 3,4 outputs, GPIO 0,1,2,5,6,7 inputs

I think that the calibration routine was the issue.

Last edited by eng1; 28th February 2007 at 05:42 PM.
eng1 is offline  
Old 28th February 2007, 05:37 PM   (permalink)
Default

It works!! Thanks!

Can I use GPIO to read from the inputs as well?

Last edited by mikkelbg; 28th February 2007 at 05:42 PM.
mikkelbg is offline  
Old 28th February 2007, 05:52 PM   (permalink)
Default

Yes, you can check an input with an if instruction. For example:
Code:
if (GPIO0==0)

     // do something
If you need pull-up resistors, the PIC12F675 has internal weak pull-up resistors that can be enabled in the option register and in the WPU register (GP3 does not have the pull-up resistor).
eng1 is offline  
Old 14th March 2007, 08:07 PM   (permalink)
Default

Great thanks!

Do you also have some code that is able to make a delay? Currently i'm using this:

void Delay(char value)
{
for (outer=value; outer != 0; outer--)
{
for (inner=0x57; inner != 0; inner--) //0xAF
{

}
}
return;
}

But isn't precise enough to control a servo motor.
mikkelbg is offline  
Old 14th March 2007, 08:11 PM   (permalink)
Default

Doesn't your C compiler have a Delay() function?.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Old 14th March 2007, 08:38 PM   (permalink)
Default

Quote:
Originally Posted by mikkelbg
Do you also have some code that is able to make a delay?
Do you need a function to generate variable delays? If not, you can place for loops with different initialization where each delay is required. You can use the Stopwatch function of MPLAB to check the accuracy.
By the way, do you want the delay to be exact? This is not always required.
If you want precise timing, you can use a TIMER and interrupts.

Last edited by eng1; 14th March 2007 at 08:41 PM.
eng1 is offline  
Old 18th March 2007, 02:52 PM   (permalink)
Default

Nigel Goodwin -> How should the Delay function work? Delay(milliseconds) ?

eng1 -> Yes it needs to be precise. I'm going to control a servo with it, so I need to be able to make a pause less than a millisecond.
mikkelbg is offline  
Old 18th March 2007, 03:01 PM   (permalink)
Default

Quote:
Originally Posted by mikkelbg
Nigel Goodwin -> How should the Delay function work? Delay(milliseconds) ?

eng1 -> Yes it needs to be precise. I'm going to control a servo with it, so I need to be able to make a pause less than a millisecond.
Then move to assembler, where you can get 1uS resolution with a 4MHz clock - or at least add an inline assembler function to do it.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Reply

Bookmarks

Thread Tools
Display Modes



Similar Threads
Title Starter Forum Replies Latest
Most Common and Best PIC? LiquidOrb24 Micro Controllers 13 12th February 2007 03:42 AM
Good Pic Book Recomendations BackFire1 Micro Controllers 13 5th February 2007 11:20 PM
PIC 16F628A UART Coding Problem Gayan Soyza Micro Controllers 11 1st February 2007 12:59 PM
Questions from a beginner in electronics... Shocks McTool General Electronics Chat 8 21st November 2005 03:01 AM
Newcomers, please read! (PIC regarded) Upd. 0xD Jay.slovak Micro Controllers 0 17th April 2005 02:05 PM



All times are GMT. The time now is 07:10 AM.


Electronic Circuits  |  Learning Electronics
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.

eXTReMe Tracker