Ejemplo n.º 1
0
void OW_loop()
{
  if ( timeout ) 
  { 
    byte i;
    byte prevTimeout;

    timeout--;
    prevTimeout = timeout;

    CLRWDT();

    // wait 10 ms or until a request was made
    for (i=40; i && (timeout == prevTimeout); i--)
      DelayUs(250);

    return;
  }

  OW_EnterSleep();
  // go to sleep after 2 second of inactivity
  CLRWDT();
  SLEEP();
  NOP();
  OW_LeaveSleep();
}
Ejemplo n.º 2
0
    void ClearNVM( WORD dest, WORD count )
    {
        BYTE    dummy = 0;
        WORD    i;
        BYTE    oldByte;

        #ifdef ENABLE_DEBUG
            ConsolePut('c');
        #endif

        for (i=0; i<count; i++)
        {

    #if defined(VERIFY_WRITE)
            NVMRead( &oldByte, dest, 1 );
            while (oldByte)
    #elif defined(CHECK_BEFORE_WRITE)
            NVMRead( &oldByte, dest, 1 );
            if (oldByte)
    #endif
            {
                NVMWrite( dest, &dummy, 1 );
    #if defined(VERIFY_WRITE)
                NVMRead( &oldByte, dest, 1 );
    #endif
            }
            dest++;
            CLRWDT();
        }
    }
Ejemplo n.º 3
0
unsigned char keys_get(void) {
    static volatile unsigned debounce = DEBOUNCEVAL;
    keypress = ~KEY_bits & SW_MASK;
    CLRWDT();
    if (keypress == prev_keypress) { //key repeated press
        --debounce;
        if (debounce)
            return KEYRELEASED; //retorna tecla solta, antes de debounce
        switch (key_state) {
            case KEYRELEASED: //retorna com valor de tecla solta
                key_state = KEYPRESS;
                debounce = KEYFIRSTREPEATVAL;
                break;
            case KEYPRESS: //debounce corrigido
                key_state = KEYPRESSLONG;
                debounce = KEYREPEATVAL;
                break;
            case KEYPRESSLONG:
                debounce = KEYREPEATVAL;
                break;
            default:
                break;
        }
        return keypress;
    }
    debounce = DEBOUNCEVAL;
    key_state = KEYRELEASED;
    prev_keypress = keypress;
    return KEYRELEASED; //nova tecla pressionada?
}
//***********************************************************
void InitBrd(void)
{
     CLRWDT();
     OSCCON=0x70;        // $70 - Primary Clock 8MHz.
     OSCTUNE=0x40;        // 4xPLL enable, 8MHz x 4xPLL=32MHz
                          // $0 - Enabled the External Oscillator.
     TRISA=0xFF;         // PortA Register = All PortA are Inputs.
     TRISB=0xFD;         // PortB Register = RB1=Output(SW_OFF) RB0=Input(LED).
     PortB=0;            // Set the voltage to the Cellular Module.
     TRISC=0xBF;         // PortC Register = RC6-Input, Other-Outputs.
     TRISD=0x00;         // PortD Register = All PortD are Outputs.
     PortD=0;            // Set PortD output = Zero.
     TRISE=0x00;         // PortE Register = All PortE are Outputs.
     PortE=0;            // Set PortE output = Zero.
     TRISF=0x06;         // PortF Register = RF1,RF2-Input, Other-Outputs.
     PortF=0;            // Set PortF output = Zero.
     TRISG=0xFD;         // PortG Register = RG1-Output, Other-Inputs.
     ADCON0=0x00;        // A/D Control Register = A/D Off.
     ADCON1=0x0f;        // Analog Select Register = All Ports are Digital.
     I2C1_Init(100000);   // initialize clock=100kHz.
     Uart1_init(9600);  // initialize USART1 module - GSM Serial Com.
     Uart2_init(9600);  // initialize USART2 module - User Serial Com.
     PortD=0x80;         // Power-up the Cellular Module.
     Delay_ml(3000);
     PortD=0x0;
     //Soft_Uart_Init(PORTA, 0, 2, 9600, 0);
     Delay_ml(9000);
}//InitBrd
Ejemplo n.º 5
0
void init()
{
    OSCCON = 0b01100010;

    UART_Init();

    initBargraph();

    initSonar();

    initBuzzer();

    song();

    CLRWDT();

    //INIT OLED
    I2C_Close();              // Close the  I2C Bus
    I2C_Init(1);             // I2C 400kHz, 20MHz-CRYSTAL
    Oled_Init();
    Oled_SetFont(Terminal12x16, 12, 16, 32,127);
    Oled_FillScreen(0x00, 0, 0);
    Oled_ConstText("SONAR", 35, 0);
    Oled_Text("!", 110, 0);
    Oled_ConstText("L=", 2, 5);
    Oled_ConstText("cm", 105, 5);
}
Ejemplo n.º 6
0
void interrupt isr(void) {
    GLOBAL_INT = 0;
    if (TMR0_INT) {
        TMR0_INT = 0;
        TMR0_RESET();
        tmr0_flag = 1;
        time++;
        CLRWDT();
    }
    if (TMR1_INT) {
        TMR1_INT = 0;
        TMR1_RESET();
        comms_time++;
        tmr1_flag = 1;
        CLRWDT();
    }
    //if (TMR2_INT) {
    //TMR2_INT = 0;
    //TMR2_RESET();
    //tmr2_flag = 1;
    //}
    if (RX1_INT) {
        RX1_INT = 0;
        rx1_buff[rx1_count] = RCREG;
        if (rx1_buff[rx1_count] == 0x0a)rx1_flag = 1;
        if (rx1_buff[rx1_count++] == '$')rx1_count = 0;
        //cahnged to looking for "$" ended by newline

        //        if (rx1_count == SUR_PACK_LEN)rx1_flag = 1;
        //        if (rx1_buff[rx1_count] == 0xff)
        //        {
        //            rx1_count = 1;
        //            rx1_255_flag=1;
        //        }
        //        if (rx1_255_flag)rx1_count++;
    }
    if (RX2_INT) {
        RX2_INT = 0;
        rx2_flag = 1;
    }
    if (LEAK1_INT) {
        LEAK1_INT = 0;
        leak1_flag = 1;
        Tomcat_TX_error(LEAK);
    }
    GLOBAL_INT = 1;
}
Ejemplo n.º 7
0
Archivo: main.c Proyecto: easlern/nerf
void sleepForABit(){
    nerf_sleep();
    SWDTEN = 1; // Enable the watchdog timer
    CLRWDT();   // Reset the watchdog timer
    SLEEP();    // Go to sleep until the watchdog timer goes off, then resume execution
    SWDTEN = 0; // Disable the watchdog timer
    nerf_wake();
}
Ejemplo n.º 8
0
void I2CM_Stop(void) {
    MSSPCON2bits.PEN = 0x01;

    while (MSSPCON2bits.PEN) {
        CLRWDT();
    }
    NOP(); //for Breakpoint
}
Ejemplo n.º 9
0
void sleep_initialize(void)
{
    VREGCON = 0x03; // lowest power mode
    CPUDOZEbits.IDLEN = 0; // full sleep mode
    
    WDTCON = 0b00011101; // 16 sec period
    CLRWDT(); // clear WDT
}
Ejemplo n.º 10
0
/**
 * Wait for a given number of milli-seconds using busy waiting scheme.
 * @param time - time in ms to wait.
 */
void wait_ms(uint16_t time)
{
    static long timel = 0;
    timel = time * 1000l; //16MHz is 4000 lines per ms. for loops takes 4 lines, so 1000 lines per ms
    for( ; timel; timel--){
        CLRWDT();
    };// no initial condition, while time is >0, decrement time each loop
}
//***********************************************************
void Delay_ml(unsigned int time_limit)
{
  unsigned int i;
  for(i=0;i<time_limit;i++)
  {
     Delay_ms(1);
     CLRWDT();
  }
}//Delay_ml
Ejemplo n.º 12
0
// http://microchip.wikidot.com/faq:29
void putch(char c)
{
#ifdef DEBUG
#ifdef WITH_HOS
    while (Busy2USART())
        CLRWDT();
    Write2USART(c);
    while (Busy2USART())
        CLRWDT();
#else
    while (Busy1USART())
        ;
    Write1USART(c);
    while (Busy1USART())
        ;
#endif
#endif
}
Ejemplo n.º 13
0
void main()
{
    char* temp;
    char number[4];
    int count = 0;

    init();

    while (1) {
        //distance = getFilteredData();
        if(SonarReady == 0) {
            distance = 1000;
        }
        else {
            distance = getData();
        }

        if (BufferReady == 1) {
            DataCommand(ScrollBuffer);
        }

        setBargraph(distance);

        if (distance > 100) {
            TRISBbits.RB3 = 1;      // Disable RB3 output
            PIE1bits.TMR1IE = 0;    // Timer1 interrupt disabled
        }
        else {
            PIE1bits.TMR1IE = 1;    // Timer1 interrupt enabled
        }

        ++count;

        if (2048 < count) {
            if (distance == 1000) {
                temp = "888";
            }
            else {
                temp = getChar(distance);
            }

            for (int i = 0; i < 4; ++i) {
                number[i] = temp[i];
            }

            Oled_SetFont(Segment_25x40, 25, 40, 46, 58);
            Oled_Text(number, 30, 3);

            DataTransfert(number);

            count = 0;
        }

        CLRWDT();
    }
}
Ejemplo n.º 14
0
uint8_t I2CM_GetByte(void) {
    MSSPCON2bits.RCEN = 1;

    while (MSSPCON2bits.RCEN) {
        CLRWDT();
    }
    NOP(); //for Breakpoint

    return MSSPBUF;
}
Ejemplo n.º 15
0
void
sleep_deep (unsigned char div)
{
	T1CON = 0;
	CLRWDT ();
	WDTCON = ((div & 0xF) << 1) | 1;
	SLEEP ();
	WDTCON = 0;
	T1CON = T1CON_DEFAULT;
}
Ejemplo n.º 16
0
void WWDT_TimerClear(void) {
    // Disable the interrupt,read back the WDTCON0 reg for arming, 
    // clearing the WWDT and enable the interrupt.
    uint8_t readBack = 0;

    bool state = GIE;
    GIE = 0;
    readBack = WDTCON0;
    CLRWDT();
    GIE = state;
}
Ejemplo n.º 17
0
Archivo: intr.c Proyecto: wheelerz/ento
void
main(void)
{
	/* setup stuff */

	T0CS = 0;		// Timer increments on instruction clock
	TRISB = 0xFE;		// Port B bit 0 is output 
	T0IE = 1;		// Enable interrupt on TMR0 overflow
	GIE = 1;		// Global interrupt enable
	for(;;)
		CLRWDT();	// Idly kick the dog
}
Ejemplo n.º 18
0
// ****************************************************************************
// main
//
int main(void)
{
    InitializeSystem();

    USBDeviceAttach();
    
    while (1) {
        updateState();
        handleKeys();
        CLRWDT();  // tickle watchdog
    }

} //end main
Ejemplo n.º 19
0
void main(void)
{
    /* Configure the oscillator for the device */
    ConfigureOscillator();

    /* Initialize I/O and Peripherals for application */
    InitApp();
    
    while(1)
    {
        CLRWDT();
    }
}
Ejemplo n.º 20
0
// nhan data tu cmr
BYTE getCommand( BYTE buffer[], WORD len){
    WORD i=0;
    for(i = 0;i<len; i++){
          buffer[i]= ConsoleGet();
          //UART1PrintChar(buffer[i]);
          CLRWDT();
         //EEPROM_Write(i,buffer[i]);

    }
  if(buffer[0] == CMD_CMR)
        return 1;
    else
        return 0;
}
Ejemplo n.º 21
0
// main function; program starts here
void main(void) 
{
    unsigned char data = 0;
    unsigned int battery_voltage = 0; // ADC reading of battery voltage
    
    ports_initialize();    // set up ports 
    ADC_initialize();      // configure ADC
    SPI_initialize();      // set up SPI module
    sleep_initialize();    // set up sleep mode
    ISR_initialize();      // set up ISR
    
    while(1)
    {
        SLEEP(); // sleep PIC until WDT expires
        
        time++; // increment time (+16s)
        
        battery_voltage = ADC_read(0x09); // read battery voltage
        
        if ( time >= 1 ) // 1*16s=16s
        {        
            time = 0; // reset time
            if(battery_voltage >= 690) //690 = 11v
            {
                LATCbits.LATC4 = 1; // turn relay on
                
                SPI_enable(); // turn on SPI module
            
                while( data != 0xDA ) // wait for shutdown command to be received
                {
                    while(!SSP1STATbits.BF) // wait till data is in the buffer
                    {
                        CLRWDT();
                    }
                    data = SSP1BUF; // read data outside
                }
            
                data = 0x00;
            
                SPI_disable(); // turn off SPI module
                
                __delay_ms(10000); // wait for R Pi to shutdown
            
                LATCbits.LATC4 = 0; // turn relay off
            }  
        }
    }    
}
//***********************************************************
int BitRdInput(int InputNum)
{
 int Result;
 TRISA=0xFF;
 TRISF=0x06;
 ADCON0=0x0;
 ADCON1=0x0f;
 CLRWDT();
 switch (InputNum)
     {
            case 1: Result=PortA&0x1;              break;
            case 2: Result=((PortA&0x2)>>1);       break;
            case 3: Result=((PortF&0x2)>>1);       break;
     }
 return Result;
}//BitRdInput
Ejemplo n.º 23
0
void buzz_off( unsigned char duration )
{
  BUZZER_A = 0;
  BUZZER_B = 0;
  while ( duration )
    {
      CLRWDT( );
      if( msec100buzz )
        {
          msec100buzz = 0;
          duration--;
          //BUZZER_A = 0;
          //BUZZER_B = 0;
        }
    }
}
Ejemplo n.º 24
0
void main(void)
{
    // initialize the device
    SYSTEM_Initialize();
    INTERRUPT_GlobalInterruptEnable();
    INTERRUPT_PeripheralInterruptEnable();
    EUSART_Initialize();
    EPWM1_Initialize();

    uint16_t dutyValue;
    uint16_t angle;
    uint8_t c;
    uint8_t buf[32] = {'\0'};
    uint8_t cnt = 0;
    
    // read angle from EEPROM
    angle = (uint16_t)DATAEE_ReadByte(0);
    // printf("read angle from EEPROM: %d\n", angle);
    dutyValue = calc_duty(angle);
    EPWM1_LoadDutyValue(dutyValue);
            
    while (1)
    {
        __delay_ms(500);
        LATCbits.LATC4 ^= 1;

        do {
            c = EUSART_Read();
            if (c == '\n') {
                buf[cnt] = '\0';
                cnt = 0;
                if (strcmp(buf, "w") == 0) {
                    DATAEE_WriteByte(0, (uint8_t)angle);
                    // printf("write the last angle onto EEPROM: %d\n", angle);
                } else {
                    angle = atoi(buf);
                    dutyValue = calc_duty(angle);
                    EPWM1_LoadDutyValue(dutyValue);
                }
            } else {
                buf[cnt++] = c;
            }
        } while (EUSART_DataReady);

        CLRWDT();
    } 
}
Ejemplo n.º 25
0
void main(void)
{
    // Hardware Initialization
    TRISIO = 0xFF; // All IO are inputs to be safe
    GPIO = 0x00; // All IO to 0
    //ANSEL = 0x00; // Turn off ADC
    CMCON = 7; // Turn off Comparators

    Init_Timers(); // Initialize Timers (TMR0)

    Init_IO(); // Initialize In-and-Outputs

    while (On) // Infinite loop
    {
        CLRWDT();
    }
}
//***********************************************************
unsigned int AnaRdInput(int InputNum)
{
     unsigned int i,Analog_Value;
     InputNum=InputNum-1;
     if (InputNum==2)InputNum=6;
     ADCON1=0x17;
     ADCON2=0x90;
     Delay_ml(5);
     for(i=0;i<5;i++)
     {
       Analog_Value=ADC_read(InputNum);
       Delay_ml(7);
     }
     ADCON0=0x0;
     ADCON1=0x0F;
     CLRWDT();
     return Analog_Value;
}//AnaRdInput
Ejemplo n.º 27
0
    void ClearNVM( NVM_ADDR *dest, WORD count )
    {
        BYTE    dummy = 0;
        WORD    i;

        for (i=0; i<count; i++)
        {
    #if defined(VERIFY_WRITE)
            while (memcmppgm2ram( &dummy, (rom void *)dest, 1 ))
    #elif defined(CHECK_BEFORE_WRITE)
            if (memcmppgm2ram( &dummy, (rom void *)dest, 1 ))
    #endif
            {
                NVMWrite( dest, &dummy, 1 );
            }
            dest++;
            CLRWDT();
        }
    }
Ejemplo n.º 28
0
void DelayMs(unsigned char cnt){
#if	XTAL_FREQ <= 2MHZ
	do {
		DelayUs(996);
	} while(--cnt);
#endif

#if    XTAL_FREQ > 2MHZ	
	unsigned char	i;
	do {
        CLRWDT( );
		i = 4;
		do {
			DelayUs(250);
		} while(--i);
	} while(--cnt);
	NOP();
	NOP();
#endif
}
Ejemplo n.º 29
0
Archivo: main.c Proyecto: wazuba/AFC
int main (void)
{
	
	while(OSCCONbits.LOCK!=1);	/* Wait for PLL to lock */


	__delay32(EEPROM_DELAY*10);	/* Wait for EEPROMs to settle */
	
	__delay32(30000000);
    state = STATE_INIT;

	while(1)
	{
		CLRWDT()
		stateMachine();	/* call state machine */
		  	
	}
	
	

}
Ejemplo n.º 30
0
/**
Operation: Wait for a byte byte on the I2C bus, send \em u8_ack2Send as the acknowledgement bit to send back to the slave.
\param u8_ack2Send ack bit to send back to slave after byte is read
\return byte read from slave
*/
uint8 getI2C2(uint8 u8_ack2Send) {
  uint8 u8_wdtState;
  uint8 u8_inByte;

  sz_lastTimeoutError = "I2C2 Get";
  u8_wdtState = _SWDTEN;              //save WDT state
  _SWDTEN = 1;                        //enable WDT
  while (I2C2CON & 0x1F);           //wait for idle condition
  I2C2CONbits.RCEN = 1;             //enable receive
  while (!I2C2STATbits.RBF);        //wait for receive byte
  CLRWDT();
  u8_inByte = I2C2RCV;              //read byte;
  //wait for idle condition before attempting ACK
  while (I2C2CON & 0x1F);           //lower 5 bits must be 0
  I2C2CONbits.ACKDT = u8_ack2Send;  //ACK bit to send back on receive
  I2C2CONbits.ACKEN = 1;            //enable ACKbit transmittion
  while (I2C2CONbits.ACKEN);        //wait for completion
  _SWDTEN = u8_wdtState;              //restore WDT
  sz_lastTimeoutError = NULL;
  return(u8_inByte);                  //return the value
}