View previous topic :: View next topic |
Author |
Message |
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Mon Nov 30, 2015 6:13 am |
|
|
couple of comments...
1) it'd be real nice to know WHAT the load or device is you're controlling. If it's a motor then no amount of clever, fast code will keep it at xx.xxxx rpm without feedback. If it's an LED x.1% or x.2% will look the same to the human eye. That's why it's important to post what you're controlling.
2) Pretty sure that PIC only goes 4MHz which is 'snail slow' these days. To gain speed which = faster calculations, more operations you might want to upgrade the PIC. Several are far more powerful AND cheaper as well !
3) Use of adc_internal. No one has pointed out that this option is used for a PIC in sleep mode. That PIC probably uses the same ADC core as the other PICs though I haven't checked so read the datasheet, ADC section and you'll see a chart of valid operational setups for the ADC
Jay |
|
|
darryl_co
Joined: 11 Nov 2015 Posts: 21 Location: India
|
Purpose of code |
Posted: Tue Dec 01, 2015 1:57 am |
|
|
what I am trying to make is a small induction heater.My plan was to drive the load coil using MOSFETS driven by MOSFET driver IC.There would be limitations with the voltage source used and the load coil resistance/impedance. So the alternative method was PWM of the signal to control conduction. I understand to get faster calculations, the method should be proper and I need to use higher clock at the cost of losing two pins.
So for I think I have got sufficient information to modify the code for ADC operation.But due to lack of time at present I plan to uses transistors in the drive stage instead of MOSFETs.I am posting the new code. Need some help on that too
Code: | #include <12F675.h>
#device ADC=10
#fuses INTRC_IO,NOWDT,NOPUT,NOPROTECT,NOCPD,MCLR
#use delay(clock=4000000)
#define GP0 PIN_A0 // Safety Switch input
#define GP1 PIN_A1 // FREQ output 1
#define GP2 PIN_A2 // FREQ output 2
#define GP3 PIN_A3 // MCLR = Reset Pin
#define GP4 PIN_A4 // LED 1
#define GP5 PIN_A5 // LED 2
#byte OSCCAL = 0x80
unsigned int step; // Output frequency step in uSec
unsigned int sdelay;
int x=0;
void init()
{
OSCCAL = 0x80; // set internal oscillator to mid frequency
set_tris_a( 0b11001001 ); // set GP0,GP3 intput, all other outputs
setup_comparator( NC_NC_NC_NC ); // disable comparators
setup_adc_ports( NO_ANALOGS ); // disable analog inputs
setup_adc( ADC_OFF ); // disable A2D
}
void alarm()
{
output_low( GP4 ); // turn GP4 off
output_low( GP5 ); // turn GP5 off
while(input(GP0)) // Loop while Safety Switch Activated
{
delay_ms(500);
output_toggle(GP4); // Sequence both GP4 and GP5 high and low
output_toggle(GP5); // high and low together
}
}
//Startup Delay
void start()
{
for(sdelay=0;step<5;step++)
{
// Sequence GP4-HIGH>>GP5-HIGH>>GP4-LOW>>GP5-LOW>>GP4-HIGH>>GP5-HIGH
delay_ms(250);
output_toggle(GP4);
delay_ms(250);
output_toggle(GP5);
}
}
void main()
{
again:
init();
// Turn Off outputs
output_low( GP1 ); // turn GP1 off
output_low( GP2 ); // turn GP2 off
output_low( GP4 ); // turn GP4 off
output_low( GP5 ); // turn GP5 off
start(); // Startup Delay
while ( 1 )
{
while(!input(GP0)) // Sweep while Safety Switch De-Activated
{
for(step=20;step<255;step++) // Loop control
{
// step=30; // Manual setting for testing/debugging
delay_us(2); // Dead Time
output_high( GP1 ); // Turn GP1 on
delay_us(step); // Time output GP1 is ON
output_low( GP1 ); // Turn GP1 off
delay_us(2); // Dead Time
output_high( GP2 ); // Turn GP2 on
delay_us(step); // Time output GP2 is ON
output_low( GP2 ); // turn GP2 off
}
// Sequence required GP4-HIGH>>GP5-LOW>>GP4-LOW>>GP5-HIGH
// But both GP4 and GP5 are LOW
if (step==255)
{
x++;
if (x==2)
{
output_low(GP5); // GP5 OFF
output_high(GP4); // GP4 ON
}
if (x==4)
{
output_low(GP4); // GP4 OFF
output_high(GP5); // GP5 ON
x=0;
}
}
}
alarm(); // Safety switch Activated
}
} |
I am unable to get GP5=!GP4
How to get the code to start from the beginning if MCLR pin is pulled LOW
Do I need to set the OSCCAL value to mid frequency
Have I set the TRIS set properly(according to user Ttelmah it is not necessary)
In C isn't it necessary to add "org 0"
Last edited by darryl_co on Tue Dec 01, 2015 3:42 am; edited 1 time in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Tue Dec 01, 2015 2:35 am |
|
|
One possibility is that the output is not actually going 'high'.
Problem is that a PIC output, if it is driving a reasonable load, won't actually go high enough to be _read_ as being high. Also any capacitance on the GP4 pin, means the signal may take time to get to the high state. So if you change a pin the instruction before you read it, there is a good chance it won't be 'seen' at the new level. So if Your load on GP4, is enough to make the signal not actually be 'seen' as high, or there is significant capacitance, then inverting like this, won't work.
However given you are the person driving the pins, it is pointless to do it this way. You are driving one pin high, drive the other low.
Code: |
if (step==254)
{
x++
if (x==2)
{
output_low(GP5); // GP5 OFF
output_high(GP4); // GP4 ON
}
if (x==4)
{
output_low(GP4); // GP4 OFF
output_high(GP5); // GP5 ON
x=0;
}
}
|
Generally with anything 'driving', you want to turn things off _before_ you turn things on. Assuming 'high' is 'on', I have reversed the order that the switching is done on the first case.
Also though, since x only changes when step==255, there is no point in actually executing the output test code, unless x has changed. I've therefore included it in the step test.
Note then that step will never ==255. Think about it 'step<255'.....
This is probably what is actually going wrong.
Then the big 'serious thing' to understand with the hardware, is that you need to work out really carefully, where the stored energy in the coils 'goes' when you switch the transistors _off_. Basically having a coil, and driving it with a transistor, then switching the transistor off, is the 'core circuit' of the voltage generator used to drive flash-guns!. The voltage on the coil when released will rise until _something_ takes the energy. On the flash gun, this is a capacitor, and after just a few cycles, the voltage can be up to hundreds of volts. On any circuit like this there has to be something to absorb this energy. A reverse biased diode across the coil for example, that 'shorts' this out. Or a separate storage capacitor being discharged, into which the energy is trapped. This part of the circuit is _vital_.
As a further comment, consider the alternative of using a hardware PWM. The problem with software, is that the pulse widths _will_ differ (unless you control them with something like the CCP), as the code handles calculations etc.. A chip like the PIC12F1840, gives you hardware PWM's in the 8pin package. Much much better for a job like this. |
|
|
darryl_co
Joined: 11 Nov 2015 Posts: 21 Location: India
|
|
Posted: Tue Dec 01, 2015 2:57 am |
|
|
Sorry but still the outputs GP4 and GP5 remain LOW(OFF) |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Tue Dec 01, 2015 3:00 am |
|
|
Have you switched to using 254, not 255?.....
Read the whole post. |
|
|
darryl_co
Joined: 11 Nov 2015 Posts: 21 Location: India
|
|
Posted: Tue Dec 01, 2015 3:15 am |
|
|
yes I made
step==254 and also tried step<255 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Tue Dec 01, 2015 3:27 am |
|
|
As your code is currently written, as soon as the two bits are set to opposite values, 'alarm' is called in the very next line, which sets them the same, and then toggles them. It is in completely the wrong place in the code. It wants to only be called when GP0 goes high.... |
|
|
darryl_co
Joined: 11 Nov 2015 Posts: 21 Location: India
|
|
Posted: Tue Dec 01, 2015 3:46 am |
|
|
Thank you once again user Ttelmah.
I modified the code and changed the call for alarm(); and now it works fine. Only thing left is, on MCLR pin being pulled low, to reset the code, start from the beginning, call function main() |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Tue Dec 01, 2015 4:32 am |
|
|
It will. |
|
|
darryl_co
Joined: 11 Nov 2015 Posts: 21 Location: India
|
|
Posted: Tue Dec 01, 2015 4:43 am |
|
|
Tested simulation in proteus. The output GP1 and GP2 just pauses. I don't get the start up delay. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Tue Dec 01, 2015 4:55 am |
|
|
Hint.
Read the sticky at the top of the forum about Proteus..... |
|
|
darryl_co
Joined: 11 Nov 2015 Posts: 21 Location: India
|
|
Posted: Tue Dec 01, 2015 7:21 am |
|
|
SORRY |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Tue Dec 01, 2015 7:50 am |
|
|
Unfortunately, the sticky is actually a bid 'pallid' compared to the real feelings here. Have a look at a few threads like this:
<http://www.ccsinfo.com/forum/viewtopic.php?t=54161&highlight=proteus>
Then realise if you are basic your design on Proteus, you are almost certainly wasting your time. You'll have to re-design the project when you actually try it in the real world.... Proteus for the PIC, is basically pointless. |
|
|
darryl_co
Joined: 11 Nov 2015 Posts: 21 Location: India
|
delay for Crystal |
Posted: Wed Dec 09, 2015 11:30 pm |
|
|
If I use a 20MHz Crystal do I need to change #use delay(clock=4M) to 20M |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Thu Dec 10, 2015 2:03 am |
|
|
You need to change the oscillator selection, and the clock. So HS instead of INTRC_IO, and set the clock to the frequency that matches the crystal.
However crystals are very rarely used with chips like the 675, since you lose 2 pins. However use an external oscillator module, and you only lose one. Use EC_IO for this. Consider instead switching to a chip like the 12F1840. Up to 32MHz from the internal oscillator, and it has a hardware PWM, which will generate signals for you far easier and the timings will remain 'right' while your code is doing other things.....
The silly thing is that these later chips are also in many cases _cheaper_!. They have hardware capsense, EUSART, MSSP, and PWM, more ROM and RAM, making them much better general 'glue' chips for little jobs.... |
|
|
|