1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| /* Main.c file generated by New Project wizard
*
* Created: mer. févr. 17 2017
* Processor: PIC16F887
* Compiler: MPLAB XC8
*/
#include <xc.h>
#define _XTAL_FREQ 4000000
#define pushed 0
#define released 1
#define empty 0
#define not_empty 1
#define ON 1
#define OFF 0
#define echo PORTCbits.RC7
#define trigger PORTCbits.RC6
#define button PORTBbits.RB0
#define sensor PORTBbits.RB1
#define motor PORTBbits.RB2
#define LED_ET PORTDbits.RD0
#define LED_MP PORTDbits.RD1
unsigned char last_button, pushed_button;
void main(void)
{
OSCCON&=0b11101110;// use internal oscillator
OSCCON|=0b01100000;// set it to 4 Mhz
TRISD=0x00;// configure PORTD as output
PORTD=0x00;// initialize PORTD to 0x00 :All LEDs are OFF
ANSELH=0x00;
OPTION_REGbits.nRBPU=0;
WPUBbits.WPUB1=1;
TRISCbits.TRISC7=1;
TRISCbits.TRISC6=0;
TRISBbits.TRISB0=1;
TRISBbits.TRISB1=1;
TRISBbits.TRISB2=0;
motor=OFF;
LED_ET=OFF;
LED_MP=OFF;
last_button=button;
pushed_button=0;
T1CON = 0x01;
float a ;
// Write your code here
while (1)
{
if((last_button==released) && (button==pushed)) pushed_button=1;
last_button=button;
if (sensor==empty)LED_ET=ON;
else LED_ET=OFF;
if (pushed_button)
{
pushed_button=0;
if(sensor==empty)
{ while(sensor==empty)
{
LED_ET=~LED_ET;
__delay_ms(500);
}
}
else
{
LED_MP=ON;
motor=ON;
while(sensor==not_empty);
LED_MP=OFF;
motor=OFF;
}
}
trigger=1; // trigger high
__delay_us(10);
trigger=0; //trigger low
__delay_ms(50);
TMR1H = 0;
TMR1L = 0;
while (!echo); //Waiting for Echo
TMR1ON=1;
while(echo); //Waiting for Echo goes LOW
TMR1ON=0;
a = (TMR1L | (TMR1H<<8));
a = a/58.82;
}
} |
Partager