Jump to content
Sign in to follow this  
Akropolis

Strange Problem With Porta On Pic 16f870

Recommended Posts

Hi, some time ago I started experimenting with PICs. Untill now I could find everything I didn't know here. My problem is that as soon as I call the beep() function porta.0 gets set to 0? I have no idea how this is possible, anyone got any ideas?

 

Thanks in advance B)

 

My partial program code:

 

/* test program */
#include <system.h>

// standaard voor de 16F serie
#pragma DATA 0x2007, _HS_OSC & _WDT_OFF & _LVP_OFF

#pragma CLOCK_FREQ 4000000

// beepie
void beep(int dur)
{
int i;

for (i = 0; i < dur; i++)
{	
	porta.5 = 1;
	delay_100us(2);// delay
	porta.5 = 0;
	delay_100us(2);// delay
}
}

void main()
{

trisa = 00000000b;
trisc = 00000000b;

trisb = 00001111b;

// one led on both ports
porta.0 = 1;
portc.3 = 1;

while (1)
{
	// if button is pressed
	if (portb.0 == 1)
	{
		beep(1000);
	}
}
}

Share this post


Link to post
Share on other sites

Depending on which 16F PIC is your target you may need some additional initialization.

 

A PIC with an ADC or comparitor or PWM may need these function blocks disabled so that the port bits can be configured for digital I/O.

Share this post


Link to post
Share on other sites
Hi, some time ago I started experimenting with PICs. Untill now I could find everything I didn't know here. My problem is that as soon as I call the beep() function porta.0 gets set to 0? I have no idea how this is possible, anyone got any ideas?

 

Sounds like the 'read, modify, write' problem

 

For further details search the relevant data sheet, microchips website or google for read, modify, write

Edited by Picxie

Share this post


Link to post
Share on other sites
Hi, some time ago I started experimenting with PICs. Untill now I could find everything I didn't know here. My problem is that as soon as I call the beep() function porta.0 gets set to 0? I have no idea how this is possible, anyone got any ideas?

 

Sounds like the 'read, modify, write' problem

 

For further details search the relevant data sheet, microchips website or google for read, modify, write

 

Thanks for the reply's B) . And I found the problem. It was the 'read, modify, write' problem.

 

New code:

 

/* test program */
#include <system.h>

// standaard voor de 16F serie
#pragma DATA 0x2007, _HS_OSC & _WDT_OFF & _LVP_OFF

#pragma CLOCK_FREQ 4000000

// beepie
void beep(int dur, int tmp_porta)
{
int i;

for (i = 0; i < dur; i++)
{	
	tmp_porta.5 = 1;
	porta = tmp_porta;
	delay_100us(2);// delay
	tmp_porta.5 = 0;
	porta = tmp_porta;
	delay_100us(2);// delay
}
}

void main()
{
int tmp_porta = 0;

trisa = 00000000b;
trisc = 00000000b;

trisb = 00001111b;


// zet alle poorten op digitaal
adcon1 = 0x0F; // eerste 4 bits op 1 zette (digitaal dus)

// one led on both ports
tmp_porta.0 = 1;
porta = tmp_porta;
portc.3 = 1;

while (1)
{
	// if button is pressed
	if (portb.0 == 1)
	{
		beep(1000, tmp_porta);
	}
}
}

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...