Beispiel #1
0
//----------------------------------------------------------------------------------
char sht_read_byte(unsigned char ack)
//----------------------------------------------------------------------------------
// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1" 
{ 
  unsigned char i,val=0;
  DATA(1);//TRISCbits.RC5 = 1;                         //release DATA-line

  for (i=0x80;i>0;i/=2)              //shift bit for masking
  {
     SCK(1);                          //clk for SENSI-BUS
     Delay10TCYx(2*sht_speed);
     if (DATA_IN) val=(val | i);     //read bit
     SCK(0);
     Delay10TCYx(sht_speed);
  }
  
  if (ack){
  	DATA(0); //PORTCbits.RC5 = 0; TRISCbits.RC5 = 0;
  } else {
  	DATA(1); //TRISCbits.RC5 = 1;
  }
                        //in case of "ack==1" pull down DATA-Line
  SCK(1);               //clk #9 for ack
  Delay10TCYx(2*sht_speed);
  SCK(0);		//release DATA-line
  DATA(1);//TRISCbits.RC5 = 1;
  return val;
}
Beispiel #2
0
//This sends the magical commands to the PCD8544
void LCDInit(void) {
  //Configure control pins as output
	TRISAbits.TRISA3 = 0;  //SCE
	TRISAbits.TRISA4 = 0;	//Reset
	TRISAbits.TRISA5 = 0;	//DC


	Delay10TCYx(255);
  //Reset the LCD to a known state
	_LCDReset = 0;
	Delay10TCYx(255);
	_LCDReset = 1; 


	LCDWrite(LCD_COMMAND, 0x21); //Tell LCD that extended commands follow
	LCDWrite(LCD_COMMAND, 0x90); //0xa5 0x95 //Set LCD Vop (Contrast): Try 0xB1(good @ 3.3V) or 0xBF if your display is too dark
			//You'll probably need to play around with the contrast value above on your screen to make it look good. Mine vary depending on the room temperature
	LCDWrite(LCD_COMMAND, 0x03); //Set Temp coefficent
	LCDWrite(LCD_COMMAND, 0x14); //LCD bias mode 1:48: Try 0x13 or 0x14
	LCDWrite(LCD_COMMAND, 0x0C); //Set display control, normal mode. 0x0D for inverse (0x0C for normal)
	LCDWrite(LCD_COMMAND, 0x20); //We must send 0x20 before modifying the display control mode
	LCDWrite(LCD_COMMAND, 0x0C); //Set display control, normal mode. 0x0D for inverse (0x0C for normal)

	
}
Beispiel #3
0
void nrf_setRxAddr(char pipe, char addr) {
    CE = nrf_CLEAR;
    Delay10TCYx(3);
    TX_ADDRESS[0] = addr;
    nrf_SPI_Write_Buf(WRITE_REG + RX_ADDR_P0 + pipe, TX_ADDRESS, TX_ADR_WIDTH);
    Delay10TCYx(3);
    CE = nrf_SET;
}
void TaskSensores(void *pdata)
{    
#if OS_CRITICAL_METHOD == 3 
    OS_CPU_SR  cpu_sr;
#endif
    struct AdcMsg *m;
    INT16U value = 0;
    INT16U ValorTeclado = 0;
	INT8U err;
	for(;;)
	{
        
        m = (struct AdcMsg *) OSMemGet(dMemory,&err);
        if(err == OS_NO_ERR){
            OSSemPend(STeclado,0,&err);
            ValorTeclado = NumeroSensores;
            OSSemPost(STeclado);       
            m->adc0 = 0;
            m->adc1 = 0;
            m->adc2 = 0;
            switch(ValorTeclado){
                case 3:
                    Delay10TCYx(100);
                    SetChanADC(ADC_CH2);
                    ConvertADC();
                    while( BusyADC() );
                    value = ReadADC();
                    m->adc2 = Temp(value);
                case 2:
                    Delay10TCYx(100);
                    SetChanADC(ADC_CH1);
                    ConvertADC();
                    while( BusyADC() );
                    value = ReadADC();
                    m->adc1 = Temp(value);                    
                case 1:
                    Delay10TCYx(100);
                    SetChanADC(ADC_CH0);
                    ConvertADC();
                    while( BusyADC() );
                    value = ReadADC();
                    m->adc0 = Temp(value);
                default:
                    break;
            }

            err = OSQPost(QueueADC0,m);
            if(err == OS_Q_FULL){
                OSMemPut(dMemory,m);
                OSSemPost(STaskTxSerial);
            }
        }else{
            OSSemPost(STaskTxSerial);
        }
		//OSSemPost(STask2);
		OSTimeDly(1);
	}
}
Beispiel #5
0
/*
 * prints out a character to the lcd display
 */
void lcdChar(unsigned char letter) {
    setGPIO(GPIOA_ADDRESS, 0x80); // RS=1, we going to send data to be displayed
    Delay10TCYx(0); // let things settle down
    setGPIO(GPIOB_ADDRESS, letter); // send display character
    // Now we need to toggle the enable pin (EN) for the display to take effect
    setGPIO(GPIOA_ADDRESS, 0xc0); // RS=1, EN=1
    Delay10TCYx(0); // let things settle down, this time just needs to be long enough for the chip to detect it as high
    setGPIO(GPIOA_ADDRESS, 0x00); // RS=0, EN=0 // this completes the enable pin toggle
    Delay10TCYx(0);
}
Beispiel #6
0
/*
 * used to send commands and settings information
 */
void lcdCommand(char command) {
    setGPIO(GPIOA_ADDRESS, 0x00); // E=0
    Delay10TCYx(0);
    setGPIO(GPIOB_ADDRESS, command); // send data
    Delay10TCYx(0);
    setGPIO(GPIOA_ADDRESS, 0x40); // E=1
    Delay10TCYx(0);
    setGPIO(GPIOA_ADDRESS, 0x00); // E=0
    Delay10TCYx(0);
}
void delay10us(int x){
    if (MNML){
        Delay10TCYx(x);
        Delay10TCYx(x);
        Delay10TCYx(x);

    }
    else{
        Delay10TCYx(x);      
    }    
}
Beispiel #8
0
/*******************************************************************************
* PUBLIC FUNCTION: delay_10us
*
* PARAMETERS:
* ~ ui_10microsecond
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* delay for for period 10 microsecond base on ui_10microsecond
* based on _XTAL_FREQ value, ui_10microsecond range from 0-65,535.
*******************************************************************************/
void delay_10us(unsigned int ui_10microsecond)
{
	for( ; ui_10microsecond > 0; ui_10microsecond --)
	{
		#if (_XTAL_FREQ == 20000000)
		Delay10TCYx(5);	//50 instruction cycle with 20MHz oscillator is 10us
		#elif (_XTAL_FREQ == 48000000)
		Delay10TCYx(12);	//120 instruction cycle with 48MHz oscillator is 10us
		#endif
	}	
}
Beispiel #9
0
char nrf_disablePipe(char pipe) {
    char pipeStatus;
    
    CE = nrf_CLEAR;
    Delay10TCYx(3);
    pipeStatus = nrf_SPI_Read(EN_RXADDR);
    pipeStatus = pipeStatus & ~(0b1 << pipe);
    nrf_SPI_RW_Reg(WRITE_REG + EN_RXADDR, pipeStatus);
    Delay10TCYx(3);
    CE = nrf_SET;
    return pipeStatus;
}
Beispiel #10
0
void lcd_write(unsigned char rs, unsigned char data)
{
    LCD_RS = rs & 1;
    LCD_RW = 0;
    LCD_DATA = data;

    Delay10TCYx(1);
    LCD_E = 1;
    Delay10TCYx(1);
    LCD_E = 0;
    Delay10TCYx(1);
}
Beispiel #11
0
void read(void) {
    unsigned char i = 0; // input_bits
    unsigned char int_count = 0;
    unsigned char array_count = 0;
    unsigned char mask = 1;
    unsigned char result;
    unsigned char tmp;
    unsigned char input_state;
    
    run_timer = 0;
    
    // shift current values in the shift register
    input_ps = 1;
    Delay10TCYx(2); // delay 312ns
    input_ps = 0;      
     
    input_states[array_count] = 0;
    for (i=0; i<input_count; i++) {
        if (int_count == 8) {            
            array_count++;
            int_count = 0;       
            input_states[array_count] = 0; 
        } 
        input_state = input_data;
                               
        input_clk = 1;        
        Delay10TCYx(2); // delay 312ns
        
        input_clk = 0;
        Delay10TCYx(2); // delay 312ns
        
        if (int_count == 0) {
            result = input_state;
        } else {
            result = input_state << int_count;            
        }    
        input_states[array_count] = input_states[array_count] | result;
                                
        int_count++; 
    }           
    
    if (array_count < MAX_BOARD_INPUTS - 2) {
        array_count++;           
    }       
    for (i=0;i<array_count;i++) {
        input_states[i] = swap_byte(input_states[i]);   
    }    
    
    run_timer = 1;
}    
Beispiel #12
0
void sendDataLED(char value){
  PIR3bits.SSP2IF = 0;
  SSP2BUF = value;
  while(!PIR3bits.SSP2IF);
  PIR3bits.SSP2IF = 0;
  Delay10TCYx(0);
}
Beispiel #13
0
unsigned int ADC_read2b(unsigned char ch)
 {
  unsigned int ad_res;
  unsigned char   guardo_porta,guardo_trisa;

  //agregado por lemos
    guardo_porta = PORTA;
    guardo_trisa = TRISA;
    
    TRISAbits.RA0=1;        //RA0 se transforma en AN0
 //   ADCON1 = 0x0E;          //selección de entradas analógicas
                            //canal 0 por defecto
 //   ADCON2 = 0x2D;
 //   ADCON0bits.ADON=1;
  
  
  
  select_ADC(ch);                                //SetChanADC(ADC_channel);
  Delay10TCYx(2);
  ADCON0bits.GO=1;                               //ConvertADC();
  while(ADCON0bits.GO);                          //while(BusyADC());  
  ad_res =ADRESH; ad_res<<=8; ad_res+=ADRESL;    //ad_res = ReadADC();  
  
  //ADCON1 = 0x0F;
  LATA = guardo_porta;
  TRISA = guardo_trisa;
  
  return ad_res;
}
Beispiel #14
0
void DelayFor18TCY( void )
{
// More than 40uS delay
// 40uS = (48000000/4) = 480 cycles
Delay10TCYx(48); // Delay of 10 x 48 oscillator cycles, same as 20 clock cycles
return;
}
void TaskTeclado(void *pdata)
{
#if OS_CRITICAL_METHOD == 3
    OS_CPU_SR  cpu_sr;
#endif
	unsigned char input = 0;
	unsigned char err;
	for(;;)
	{
    
		input = Input0;
		Delay10TCYx( 100 );
		if(input != 1 && Input0 != 1){
			while(Input0 == 0){
				OSTimeDly(2);
			}
			OSSemPend(STeclado,0,&err);
			NumeroSensores--;
			if(NumeroSensores < 0){
				NumeroSensores = 3;
			}
			OSSemPost(STeclado);
		}
		OSTimeDly(50);
	}
}
Beispiel #16
0
int main(int argc, char** argv) {
    char test = 0x51; //0x96;

//    // set the MSSP1 SCL1 for output?
//
//
//    // set pins RC14, RC15 as inputs
//    TRISCbits.TRISC3 = 1; // SCL1
//    ANSELCbits.ANSC3 = 0;
//
//    TRISCbits.TRISC4 = 1; // SDA1
//    ANSELCbits.ANSC4 = 0;
//
//    // configure i2c1 for master mode @ 100 kHz
//    CloseI2C1();
//    OpenI2C1(MASTER, SLEW_OFF);
//    SSPADD = BD_RT;
    setupOutgoing(); // enable MSSP1

    while (1) {

        startAndWrite(&test);
        Delay10TCYx(20);

    }

    return (1);
}
void main(void) {
    const short m = 31;
    volatile short fr[31];
    overlay volatile short fi[31];
    struct Classification classification;
    setup();
    LATCbits.LATC2 = 1;
    while(1) {
        ISRflag = 0;
        LATCbits.LATC0 = 0;
        // fill fr and/or fi from ADC (details to come)
        fix_fft(fr, fi, m);
        classification = *classify(fr, fi);
        // if something set vibrate pin
        // print classification(s) to LCD
        while(1) {
            Delay10TCYx(1);
            LATCbits.LATC1 = 1;
            if(ISRflag) {
                break;
            }
            LATCbits.LATC1 = 0;
        }
    }
}
unsigned int getDistance_mm(){
	// On emet une impulsion 
	TRIS_SONIC = 0; // Patte en sortie
	SONIC = 1;    // Etat haut

	Delay10TCYx(12);
	// On arrete l'impulsion
	SONIC = 0;    // Etat bas
	// On écoute le port
	TRIS_SONIC = 1; // Patte en entrée
	Delay1TCY();
	// On attend que le capteur commence son echelon
	while(SONIC == 0);

	// On compte le temps pendant lequel le capteur attend
	distance_cs = getTemps_cs();
	distance_micro_s = getTemps_micro_s();

	// On attends que l'echellon se termine
	while(SONIC == 1);

	distance_micro_s = getTemps_micro_s() - distance_micro_s ;
	if(distance_cs != getTemps_cs()){
		distance_micro_s = 10000 * (getTemps_cs()-distance_cs)+ distance_micro_s;
	}
	return (unsigned int) (distance_micro_s / 6.4);
}
Beispiel #19
0
// Reading data
int readData (int adx) {
    int myRead = 0;
    SRAMsetUp();
    setUpOut();

    // Setting up Address
    PORTAbits.RA0 = (adx >> 0) & 0x01;
    PORTAbits.RA1 = (adx >> 1) & 0x01;
    PORTAbits.RA2 = (adx >> 2) & 0x01;
    PORTAbits.RA3 = (adx >> 3) & 0x01;
    PORTAbits.RA4 = (adx >> 4) & 0x01;
    PORTAbits.RA5 = (adx >> 5) & 0x01;
    PORTCbits.RC0 = (adx >> 6) & 0x01;
    PORTCbits.RC1 = (adx >> 7) & 0x01;

    // Store in MAR
    PORTBbits.RB3 = 0;
    PORTBbits.RB3 = 1;

    // I/O are inputs
    setUpIn();

    // Output Enable
    PORTBbits.RB5 = 0;
    Delay10TCYx(5);
    // Get the first 6 bits of Port A and the first 2 bits of port C shifted
    myRead = (PORTA & 0x3F) | ((PORTC << 6) & 0xC0);
//    Delay10TCYx(5);
    // Output Enable
    PORTBbits.RB5 = 1;

    return myRead;

}
Beispiel #20
0
/* switch to ADC chan 8 (shorted to ground) to reset ADC measurement cap to zero before next measurement */
void ADC_zero(void)
{
	ClrWdt(); // reset the WDT timer
	SetChanADC(ADC_CH8); // F3 grounded input
	Delay10TCYx(ADC_CHAN_DELAY);
	ConvertADC();
	while (BusyADC());
}
Beispiel #21
0
void DelayFor18TCY(void)
 {      //Delay10TCYx(0x2); //delays 20 cycles
 	Delay10TCYx(1);
	Delay1TCY();Delay1TCY();Delay1TCY();Delay1TCY();
	Delay1TCY();Delay1TCY();Delay1TCY();Delay1TCY();
        Delay1TCY();Delay1TCY();
 return;
 }
void squarewave(unsigned char m)
{ unsigned short i,n=16;//16 samples

while (SWITCH_1==1) //if RA5 not pressed
{for(i=0;i<=n;i++)
{
PORTD=0b00000000;// output low level
Delay10TCYx(m);
}
for(i=0;i<=n;i++)
{
PORTD=0b11111111;//output high level
Delay10TCYx(m);
}

}
}
Beispiel #23
0
void openI2CLED(char addr) {
     //Clear interrupt flag
     PIR3bits.SSP2IF = 0;
     //Start Enable Bit
     SSP2CON2bits.SEN = 1;
     while(!PIR3bits.SSP2IF);
     //Clear interrupt flag
     PIR3bits.SSP2IF = 0;
     Delay10TCYx(0);
     //Send address and Write bit
     SSP2BUF = (addr << 1);
     while(!PIR3bits.SSP2IF);
     //Clear interrupt flag
     PIR3bits.SSP2IF = 0;
     //Delay10TCYx(100);
     Delay10TCYx(0);
}
Beispiel #24
0
/*--------------------------------------------------------\
| MReceiveLodeSPI                                          |
|     													   |
| Takes the Slave number and, much as the SPIReassembleLode|
| is simply a repeated application of the MReceiveSPI func-|
| tion; places the results in a global variable to be util-|
| ized elsewhere;                                          |
\---------------------------------------------------------*/
void MReceiveLodeSPI(unsigned char Slave)
{
    unsigned int x;
    SelectSlaveStart(Slave);
    Delay10TCYx(30); //250 TCY Delay;
    for (x = 0; x < ReceiveLodeSize; x++)
        ReceiveLode[x] = MReceiveSPI(Slave); //Read data from slave;
    SelectSlaveEnd(Slave);
}
Beispiel #25
0
void Pas(){
	CLOCK =0;
	// On attend 1 µs, 12 cycles
	Delay1TCY();
	Delay1TCY();
	Delay10TCYx(1);
	// On repasse à 1
	CLOCK =1;
}
Beispiel #26
0
/******************************************************************************
 *	int pot1()
 *	This function is for the potentiometer that is fixed on the circuit board, 
 *  and it converts the value read from the potentiometer, which is analog, and
 *  turns it into two digital values. From there it is displayed onto LCD of the
 *  QwikFlash.
******************************************************************************/
long pot1()
{
    long adLow1;
    long adHigh1;
    long addbit1;
    long addbit2;
    unsigned char fnum1;
    unsigned char snum1;
    unsigned char tnum1;
    ADCON0 = 0b00100001;                    // FOSC / 4, Channel 7, A/D module turned on
    ADCON1 = 0b11000000;                    // Right justified, FOSC/4
    
    Delay10TCYx(4);
    ADCON0bits.GO = 1;                      // Turn on ADC
    Delay10TCYx(4);
    
    adHigh1 = ADRESH;
    adLow1 = ADRESL;
    
    addbit1 = (adHigh1 << 8) + adLow1;      // Shift adHighy1 to the left 8 bits
                                            // and add adLow1 bits.
    addbit1 = ((addbit1 * 98) / 1023) + 2;  // Make addbit1 a 2 digit value.
    addbit2 = addbit1;                      // Store unedited version of addbit1
    
    tnum1 = addbit1 / 100;                  // Store the third digit in tnum1
    addbit1 = addbit1 % 100;
    fnum1 = addbit1 / 10;                   // Store the first digit in fnum1
    snum1 = addbit1 % 10;                   // Store the second digit in snum1
    
    Str_2[1] = 'F';
    Str_2[2] = 'R';
    Str_2[3] = 'E';
    Str_2[4] = 'Q';
    Str_2[5] = ' ';
    Str_2[6] = tnum1 + 48;      // Add 48 to get the ascii value
    Str_2[7] = fnum1 + 48;      // Add 48 to get the ascii value
    Str_2[8] = snum1 + 48;
    
    ADCON1 = 0b10001110;
    DisplayC(Str_2);
    Delay(250);
    
    return addbit2;
}
Beispiel #27
0
/*--------------------------------------------------------\
| MReceiveStrSPI                                           |
|     													   |
| Takes the Slave number (which is then passed to the      |
| MReceiveSPI function and receives the characteristic 4   |
| byte double/checksum code from the Slave via SPI;        |
\---------------------------------------------------------*/
void MReceiveStrSPI(unsigned char Slave)
{
    unsigned char x;

    SelectSlaveStart(Slave);
    Delay10TCYx(30); //300 TCY Delay;
    for (x = 0; x < 4; x++)
        DoubleSPIM[x] = MReceiveSPI(Slave); //Read data from slave;
    SelectSlaveEnd(Slave);
}
Beispiel #28
0
// 38KHz のデューティー比 33% の HI で待つ
// HI:105.263157894737[cycle] == 8.77192982456142[us]
// 38KHz のデューティー比 50% の 場合
// HI:157.894736842105[cycle] == 13.1578947368421[us]
void DelayIRFreqHi(void)
{
    // duty 1/3
    Delay10TCYx(10);
    Delay1TCY();
    Delay1TCY();
    Delay1TCY();
    Delay1TCY();
    Delay1TCY();
}
/********************************************************************
*       Function Name:  Delay_100msX                               	*
*       Return Value:   void                                        *
*       Parameters:     miliseconds: amount of delay				*
*       Description:    This routine generates various delays 		*
*						needed by xlcd functions.					*
*						For delay of 100ms							*
*						(18F4431 Fosc at 20MHz)						*
*						Cycles = (TimeDelay * Fosc) / 4				*
*						Cycles = (100ms * 20MHz) / 4				*
*						Cycles = 500,000							*
*						Since call of function also takes some 		*
*						instruction cycles, the exact value to get	*
*						1ms delay is less than 500,000.				*
********************************************************************/
void Delay_100msX (unsigned int msec)
{
	t=0;
	while(t<msec)
	{
		Delay10KTCYx(49);
		Delay1KTCYx(9);
		Delay10TCYx(96);
		t++;
	}
}//End of Delay_100msX
Beispiel #30
0
unsigned int ADC_read(unsigned char ADC_channel)
 {
  unsigned int ad_res;
	   
  SetChanADC(ADC_channel);
  Delay10TCYx(5);
  ConvertADC();
  while(BusyADC());
  ad_res = ReadADC();
  return ad_res;
}