Hi,
I am using the PIC12F1571 MCU in our latest design. It is connected to two sensors (TSSP58038- Proximity, IRA-S210ST01- PIR sensor) with analog input.
The PIR is connected to RA0 and the Proximity sensor is connected to RA2.
The program:
As soon as the PIR sensor detects anything(Goes to 3V), the program should turn on the PWM(Pin RA1) and then reads the second sensor from the RA2 pin. If RA2 is over 3V, then it should toggle the Pin RA4.
Hardware:
I am using the Curiosity kit(DM164137)
The problem.
As soon as the sensor detects anything, the program which is being run on the MPLABX is halted. The codes seem it doesn't get into the second if.
Since I have spent over a week on this, I am running out of ways to test, so anyone has any suggestion?
Codes are below
Mod edit: code tags - JohnInTX
I am using the PIC12F1571 MCU in our latest design. It is connected to two sensors (TSSP58038- Proximity, IRA-S210ST01- PIR sensor) with analog input.
The PIR is connected to RA0 and the Proximity sensor is connected to RA2.
The program:
As soon as the PIR sensor detects anything(Goes to 3V), the program should turn on the PWM(Pin RA1) and then reads the second sensor from the RA2 pin. If RA2 is over 3V, then it should toggle the Pin RA4.
Hardware:
I am using the Curiosity kit(DM164137)
The problem.
As soon as the sensor detects anything, the program which is being run on the MPLABX is halted. The codes seem it doesn't get into the second if.
Since I have spent over a week on this, I am running out of ways to test, so anyone has any suggestion?
Codes are below
C:
#include <xc.h>
#define _XTAL_FREQ 20000000
#include "newxc8_header.h"
#include <stdio.h>
#include <stdlib.h>
#include <pic12f1571.h>
#define PIR RA0
#define PROX_PWM RA1 //FOR PROXIMITY SENSOR it's the pwm signal
#define PROX_IN RA2 //the output pin of the proximity sensor
int adc_resultPIR;
int adc_resultPROXI;
void toggle() {//toggle output on RA4
LATAbits.LATA5 = ~LATAbits.LATA5;
__delay_ms(10);
}
void setADC0() {
adc_resultPIR = 0;
ADCON0 = 0x01;
// ADFM left; ADPREF VDD; ADCS FOSC/2;
ADCON1 = 0x80;
// TRIGSEL no_auto_trigger;
ADCON2 = 0x00;
// ADRESL 0;
ADRESL = 0x00;
// ADRESH 0;
ADRESH = 0x00;
ADCON0bits.CHS = 0x00;
// Turn on the ADC module
ADCON0bits.ADON = 1;
// Acquisition time delay
__delay_ms(2);
// Start the ADC conversion
ADCON0bits.GO_nDONE = 1;
// Wait for the conversion to finish
while (ADCON0bits.GO_nDONE)
{
}
// Conversion finished, return the result
adc_resultPIR = ((ADRESH << 8) + ADRESL);
}
void setADC1() {//for proximity sensor
adc_resultPROXI = 0;
ADCON0 = 0x09;
// ADFM left; ADPREF VDD; ADCS FOSC/2;
ADCON1 = 0x80;
// TRIGSEL no_auto_trigger;
ADCON2 = 0x00;
// ADRESL 0;
ADRESL = 0x00;
// ADRESH 0;
ADRESH = 0x00;
// Turn on the ADC module
ADCON0bits.ADON = 1;
// Acquisition time delay
__delay_ms(2);
// Start the ADC conversion
ADCON0bits.GO_nDONE = 1;
// Wait for the conversion to finish
while (ADCON0bits.GO_nDONE)
{
}
// Conversion finished, return the result
adc_resultPROXI = ((ADRESH << 8) + ADRESL);
}
void setupPWM() {
// PWM 1 Setup
PWM1CLKCON = 0x00; // 32 prescaler, CLK: Fosc
PWM1PR = 526; // period 500ms
//PWM1DC = 7811; // Duty cycle
PWM1DC = 368;
PWM1INTE = 0x00;
PWM1INTF = 0x00;
PWM1PH = 0; // Phase counter 0
PWM1OFL = 0x01; // PWM1OFL 1;
PWM1CON = 0xC0; // MODE standard_PWM; POL active_hi; OE enabled; EN enabled;
PWM1LDCONbits.LDA = 1; // Load OF, PH, DC, and PR buffers
PWM3CON = 0x00;
}
void main(void) {
TRISA = 0b00000101;
ANSELA = 0x05;
while (1) {
setADC0();
__delay_ms(100);
if (adc_resultPIR > 600) //>620 for PIR sensor 3V
{
setupPWM();
__delay_ms(100); //Wait
setADC1();
__delay_ms(100);
if(adc_resultPROXI > 820) //820 for PROXI sensor 4V
{
toggle();
PWM1CON=0x00;
__delay_ms(10);
}
//toggle();
}
else{
PWM1CON=0x00;
}
}
}