View previous topic :: View next topic |
Author |
Message |
Roberto da Rivoli
Joined: 09 Feb 2004 Posts: 37 Location: TURIN -Italy-
|
|
Posted: Sun Jan 22, 2017 1:08 pm |
|
|
I inserted a DELAY exaggerated 1s to avoid taking the second front of the OFF-ON-OFF button as I analyze the state in the "while" loop, but I did not get improvements.
Code: | void read_Puls(void)
{
if(READ_HALL==0){
if(old_HALL==1){
if(stato==0){
set_pwm1_duty(1000); // OFF *********1000**************
stato=1;
delay_ms(200);
Stato_Sleep=1;
}
else if(stato==1){
|
Code: |
//port_a_pullups(0x20); // Enable pullups on pins GP5
I have to keep this line commented in "while" loop otherwise the program crashes
while(1)
{
if(Stato_Sleep==1){
//port_a_pullups(0x20); // Enable pullups on pins GP5
delay_us(10);
disable_interrupts(GLOBAL); //so no handler is needed
temp8=input(PIN_A5); //ensure this pin is read
clear_interrupt(INT_RA); //This is where 'RA' without the mask is used
enable_interrupts(INT_RA5); //now enable on the one pin
sleep();
delay_cycles(1); //The instruction after a sleep should be a NOP
Stato_Sleep=0;
}
read_Puls();
}
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jan 22, 2017 1:30 pm |
|
|
I'm not at a place where I have my hardware test setup. I'll try working
on it in hardware later, in the evening. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jan 23, 2017 1:24 am |
|
|
I got it to apparently work OK. It goes through the states repeatedly and
reliably. All state changes occur only on the down-press of the button.
It goes to sleep and stays in sleep until the button is pushed again.
I don't guarantee this is the best way to do. I just made it work.
I used a 12F615 on the Microchip Low Pin Count board for this test.
Pin A2 is connected to LED C0. This is the PWM LED.
Pin A4 is connected to LED C1. This is the Sleep LED. "On" = sleeping.
Code: |
#include<12F615.h>
#FUSES INTRC //Internal RC Osc
#FUSES NOPUT //Power Up Timer
#FUSES MCLR //Master Clear pin enabled
#FUSES NOPROTECT //Code not protected from reading
#FUSES IOSC8 //INTOSC speed 8MHz
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOWDT
#use delay(int=8M)
#use pwm(CCP1,TIMER=2,FREQUENCY=20000,DUTY=100)
//#use rs232(baud = 115000, xmit=PIN_A4)
#define uint8 unsigned int8
#define sint8 signed int8
#define uint16 unsigned int16
#define sint16 signed int16
#define uint32 unsigned int32
#define sint32 signed int32
#define SEL_1 PIN_A4
#define SEL_2 PIN_A0
#define SEL_3 PIN_A1
#define HALL PIN_A5
#define PWM PIN_A2
#define SLEEP_LED PIN_A4
#define READ_SEL_1 input(SEL_1)
#define READ_SEL_2 input(SEL_2)
#define READ_SEL_3 input(SEL_3)
#define READ_HALL input(HALL)
void Set_PORT(void);
void read_Puls(void);
uint8 Stato_Sleep=0;
int8 button_state = 1;
#int_ra
void ra_isr(void)
{
button_state = input(HALL);
if(button_state == 0) // If button is down, disable IOC interrupts
disable_interrupts(INT_RA5);
}
//----------------------------------
uint8 old_Hall = 1;
uint8 stato = 1;
void read_Puls(void)
{
int8 current_Hall;
current_Hall = input(HALL);
// If button is down, execute this block.
if(current_Hall == 0)
{
if(old_Hall == 1)
{
if(stato == 0)
{
set_pwm1_duty(100); // Bright LED shows in state 0
stato = 1;
Stato_Sleep = 1;
}
else if(stato == 1)
{
set_pwm1_duty(0); // LED off shows in state 1
stato = 2;
}
else if(stato == 2)
{
set_pwm1_duty(5); // Dim LED shows in state 2
stato=0;
}
}
}
old_Hall = current_Hall;
delay_ms(10);
}
//=========================================
uint8 temp8=0;
void main()
{
Set_PORT();
enable_interrupts(GLOBAL);
while(TRUE)
{
read_Puls();
if(Stato_Sleep == 1)
{
output_high(SLEEP_LED); // Show we're in sleep code
while(input(PIN_A5) == 0); // Wait until switch is released
delay_us(10);
temp8=input(PIN_A5);
clear_interrupt(INT_RA);
enable_interrupts(INT_RA5);
sleep();
delay_cycles(1);
Stato_Sleep=0;
output_low(SLEEP_LED); // Show that we woke up from sleep
}
}
}
//------------------------------
void Set_PORT(void)
{
output_a(0x00);
output_float(PIN_A5);
port_a_pullups(0x20);
delay_us(20);
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Mon Jan 23, 2017 3:22 am |
|
|
It is a little pointless to use the actual interrupt call, but the key thing is that he is using this to disable the IOC from actually happening on the key 'release'. If the key is down, he turns off the IOC in the handler, and he makes sure the sleep is not actually called until the key is released.
Key point is understanding that the interrupt is 'on change', so that it'll happen twice (at least - more with key bounces), on every key operation, and that you can't really go to sleep till the key is in the released position..... |
|
|
Roberto da Rivoli
Joined: 09 Feb 2004 Posts: 37 Location: TURIN -Italy-
|
[SOLVED] Pic12f615 sleep dont Work |
Posted: Mon Jan 23, 2017 12:52 pm |
|
|
With the solution proposed by "PCM programmer" seems to work properly, I introduced some modifications to match the applications.
Thanks "PCM programmer" and other programmers for support. |
|
|
|