Maker Pro
Maker Pro

Design Simple microcontroller circuit

sid2286

Aug 24, 2011
102
Joined
Aug 24, 2011
Messages
102
i am Trying to build a very basic circuit, the working is as follows:

operating voltage is 3.3Vdc

1. Press switch 1- LED starts to Toggle
2. Press Switch 2- Stop the toggle
3. Long press Switch 2- LED blink 5 times.

i used 18F4520 and it worked (But a costly solution) so i switched to 12F510 but not able to program., not able to figure out the issue.

Can someone kindly help me with the code or any analog circuit.

#include <18f4520.h>
#fuses INTRC,NOWDT,MCLR //,PROTECT
#use delay(clock=8000000)

void main()
{
while(True)
{
if ( input(PIN_B2) == 0)
{
while(True)
{
output_toggle(Pin_B0);
delay_ms(500);
if( input(Pin_B1) == 0)
break;
}
}
output_low(Pin_B0);

if(input(Pin_B1) == 0 && delay_ms(3000))
{

output_high(Pin_B0);
delay_ms(100);
output_low(Pin_B0);
delay_ms(100);
output_high(Pin_B0);
delay_ms(100);
output_low(Pin_B0);
delay_ms(100);
output_high(Pin_B0);
delay_ms(100);
output_low(Pin_B0);
delay_ms(100);
}
}
}
 
Last edited:

Sjoert de Boer

Aug 31, 2016
3
Joined
Aug 31, 2016
Messages
3
Hello!
To make this circuit in analog components solely, it would be rather costly.
For the C-code, its nice to use functions (in combination with hardware interrupts.) For switch 1,2:
the switch not being debounced might give problems when using a hw interrupt.

bool sw1 = 'value switch 1';
bool sw2 = 'value switch 2' ;

//LED blink as long as sw1 is engaged
void toggle_sw1( sw1 && !sw2){
led =! led;
delay_ms(1000);
}
// a do while loop can also be used within main or some function
do
led = ! led;
delay_ms(1000);
while(sw1 & !sw2);

For switch 5 you can make a counter with for example a for -loop.

for(int j= 0; j < 6; J++){
J++;
delay_ms(1000);
if(j == '5'){
blink_five_times();
}
}


void blink_five_times(){
for(int i=0; i <6; i++){
led=!led;
delay_ms(1000);
}
}

I have not personally test this code, but I hope it might point in the good direction :). I have included two ways to debounce switches. It is also possible to use a software interrupt.
you can for example read the state of the switch a few times, and have it run trough a 500 ms loop. and look for a stable reading.
 

Attachments

  • debounce.jpg
    debounce.jpg
    65.2 KB · Views: 94

CDRIVE

Hauling 10' pipe on a Trek Shift3
May 8, 2012
4,960
Joined
May 8, 2012
Messages
4,960
This topic needs to be relocated to its pertinent forum. That would be Microcontrollers & Programming.

Chris
 

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
so i switched to 12F510 but not able to program.,
Hi sid, can you elaborate on what specifically happened. You said that you are not able to program the 12F510 - are you having issues getting the chip configured? or getting any output from the chip?
 

OBW0549

Jul 5, 2016
157
Joined
Jul 5, 2016
Messages
157
You say you switched from the 18F4520 to the 12F510, but the code you posted still uses the #include directive for the 18F4520. That might be the problem.
 

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
You say you switched from the 18F4520 to the 12F510, but the code you posted still uses the #include directive for the 18F4520. That might be the problem.
Exactly. The pin outs are probably not even the same. Hopefully sid will be back on to elaborate on the exact problem.
 

sid2286

Aug 24, 2011
102
Joined
Aug 24, 2011
Messages
102
Hello,

I did make the necessary changes such as required header file to #include <18f4520.h> , i used the 8 pin IC and made the necessary cirucit as per the pinout of the IC.

while i was programming using pickit3 i was getting an error for OSSCAL but ignoring that i still programmed and it did program successfully.
i used http://ww1.microchip.com/downloads/en/DeviceDoc/PICkit3%20Programmer%20Application%20v3.10.zip
programming.

i tried to simulate the same on proteus as well but still didnt work.

i guess the IC is not getting programmed.

Request you to help.

thanks,
SId
 

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
Hello,

I did make the necessary changes such as required header file to #include <18f4520.h> ,
SId

Yes, but didn't you say that you changed from the 18f to the smaller 12f pic? If you did then you would have to call the header for the 12f pic and not the 18f. They are not compatible, the setup for each chip is separate.
 

sid2286

Aug 24, 2011
102
Joined
Aug 24, 2011
Messages
102
This is the error I am getting, everytime I am trying to program.
 

Attachments

  • IMG1.JPG
    IMG1.JPG
    176.2 KB · Views: 92

davenn

Moderator
Sep 5, 2009
14,254
Joined
Sep 5, 2009
Messages
14,254
This is the error I am getting, everytime I am trying to program.

can you please respond to chopnhack's comments in post #10
you have not stated that you understood his comment and acted on it
 

sid2286

Aug 24, 2011
102
Joined
Aug 24, 2011
Messages
102
Yes, but didn't you say that you changed from the 18f to the smaller 12f pic? If you did then you would have to call the header for the 12f pic and not the 18f. They are not compatible, the setup for each chip is separate.
Yes Sorry I completely skipped this one, Yes i did change the header file to 12F510 , while programming.

I posted the code which was working for 18F4520. so yes i did change the header file.
 

Amar Dhore

Dec 2, 2015
129
Joined
Dec 2, 2015
Messages
129
I do not see the pin assignment.

You have to define the direction of the pin (IN/OUT) and then clear any Analog (ANSEL) registers associated with the Pins.
 

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
Ok, good. Now with the pic12's the oscal value can be lost during programming - especially if you are not using mplabs ide. What programmer and ide are you using?
 

NorthGuy

Mar 24, 2016
53
Joined
Mar 24, 2016
Messages
53
Do you really want to battle these OSCCAL (and other) problems with 10-year old chips? Why not to try PIC12F1501?
 

sid2286

Aug 24, 2011
102
Joined
Aug 24, 2011
Messages
102
Ok, good. Now with the pic12's the oscal value can be lost during programming - especially if you are not using mplabs ide. What programmer and ide are you using?
Initially i was trying MPLAB IDE but since it was not working i used, pickit3 standalone software which i had shared previously.
i read multiple forums wherein most of them suggest pickit 3 is a problem while using microchips baseline controllers.

i am clueless on how to go about with it now. :(
 

sid2286

Aug 24, 2011
102
Joined
Aug 24, 2011
Messages
102
I do not see the pin assignment.

You have to define the direction of the pin (IN/OUT) and then clear any Analog (ANSEL) registers associated with the Pins.
Thanks Amar, the CCS complier internally handles the direction.
 

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
There is your issue - when using mplabs, osscal is preserved. Third party programs do not, AFAIK. The chip might be wasted at this point. Do a web search for resetting osscal and see what comes up.
 
Top