unsigned char initRTCmodule(unsigned char devADDR){ unsigned char btemp; Nop(); Nop(); // I2C_Close(); I2C_Init(39); // I2C 100 KHZ, 16 MHZ OSC (see note in "myI2C.c") Nop(); Nop(); // I2C_Start(); I2C_Idle(); btemp = I2C_WriteByte(devADDR); Nop(); Nop(); Nop(); if (btemp != 0){ Nop(); Nop(); I2C_Stop(); I2C_Close(); return 0xFF; } else { btemp = DS3231_GetInfo(0x0F); Nop(); Nop(); return btemp; }; }
/* Set Factory I2C device address */ void Set_Factory(void){ int fd; unsigned char buff[5]; unsigned char data[5]; unsigned char adrs; for (adrs = 0; adrs < 129; adrs++) { buff[0]=0x20; data[0]=0x00; /* Open I2C-BUS */ I2C_Open(&fd, adrs); if (1) { /* Write register */ I2C_Send(&fd, buff,1 ); /* Read the ADC */ I2C_Read(&fd, data, 1); if (data[0]==0x23) { /* Close I2C-BUS */ I2C_Close(&fd); printf("Found with I2c address 0x%02x \n", adrs); buff[0]=0xF0; buff[1]=0x21; /* value: * New Address to be setted * / /* Open I2C-BUS */ I2C_Open(&fd, adrs); /* Write register */ I2C_Send(&fd, buff,2 ); /* Close I2C-BUS */ I2C_Close(&fd); printf("Reverted to 0x21\n"); } /* Close I2C-BUS */ I2C_Close(&fd); } } }
uint8_t IO2::readID() { int fd; unsigned char buff[1] = {GET_ID}, id, result; I2C_Open(&fd, address); I2C_Send(&fd, buff, 1); I2C_Close(&fd); I2C_Open(&fd, address); I2C_Read(&fd, &id, 1); I2C_Close(&fd); return id; }
uint8_t IO2::digitalRead(uint8_t pin) { int fd; unsigned char buff[1] = {GET_PORT}, data, result; I2C_Open(&fd, address); I2C_Send(&fd, buff, 1); I2C_Close(&fd); I2C_Open(&fd, address); I2C_Read(&fd, &data, 1); I2C_Close(&fd); return !!(data & pin); }
/* Read Board Firmware Version */ void ReadSV(void) { int fd; unsigned char buff[5]; unsigned char data[5]; buff[0]=0x21; data[0]=0x00; /* Open I2C-BUS */ I2C_Open(&fd, 0x21); /* Write register */ I2C_Send(&fd, buff,1 ); /* Read the ADC */ I2C_Read(&fd, data, 1); printf("Firmware Version: %d.0.%2d\n", data[0]>>4,data[0]&0x0f); /* Close I2C-BUS */ I2C_Close(&fd); }
/* Read Board ID */ void ReadID(void) { int fd; unsigned char buff[5]; unsigned char data[5]; buff[0]=0x20; data[0]=0x00; /* Open I2C-BUS */ I2C_Open(&fd, 0x21); /* Write register */ I2C_Send(&fd, buff,1 ); /* Read ID */ I2C_Read(&fd, data, 1); printf("ID: 0x%x\n", data[0]); /* Close I2C-BUS */ I2C_Close(&fd); }
/* Set pin directions */ void Set_TRIS(unsigned char value){ int fd; unsigned char buff[5]; buff[0]=0x01; buff[1]=value; /* value: * 0b0ddddddd * bit0 coresponding to GPIO0, * as "1" set pin as input "0" - set pin as output. * / /* Open I2C-BUS */ I2C_Open(&fd, 0x21); /* Write register */ I2C_Send(&fd, buff,2 ); /* Close I2C-BUS */ I2C_Close(&fd); }
/* Set GPIO Pull Ups */ void Set_PU(unsigned char value){ int fd; unsigned char buff[5]; buff[0]=0x04; buff[1]=value; /* value: * 0b000dxddd * bit0 coresponding to GPIO0, * as "1" enables the pullup and "0" - disables it. * / /* Open I2C-BUS */ I2C_Open(&fd, 0x21); /* Write register */ I2C_Send(&fd, buff,2 ); /* Close I2C-BUS */ I2C_Close(&fd); }
/* Set Relay states */ void Relay(unsigned char value){ int fd; unsigned char buff[5]; buff[0]=0x40; buff[1]=value; /* value: * should be iterpreted as binary * b0 is relay 1 * b1 is relay 2 * 0 turn of relay * 1 turn on relay * / /* Open I2C-BUS */ I2C_Open(&fd, 0x21); /* Write register */ I2C_Send(&fd, buff,2 ); /* Close I2C-BUS */ I2C_Close(&fd); }
/* Read GPIO Digital Inputs */ void ReadGPIO(void) { int fd; unsigned char buff[5]; unsigned char data[5]; buff[0]=0x03; data[0]=0x00; /* Open I2C-BUS */ I2C_Open(&fd, 0x21); /* Write register */ I2C_Send(&fd, buff,1 ); /* Read the ADC */ I2C_Read(&fd, data, 1); printf("GPIO: 0x%02x\n", data[0]); /* Close I2C-BUS */ I2C_Close(&fd); }
/* Use this function to find module i2c address */ void BusScan(void) { int fd; unsigned char buff[5]; unsigned char data[5]; unsigned char adrs; for (adrs = 1; adrs < 129; adrs++) { buff[0]=0x20; data[0]=0x00; /* Open I2C-BUS */ I2C_Open(&fd, adrs); if (1) { /* Write register */ I2C_Send(&fd, buff,1 ); /* Read the ADC */ I2C_Read(&fd, data, 1); if (data[0]==0x23) printf("Found with I2c address 0x%02x \n", adrs); /* Close I2C-BUS */ I2C_Close(&fd); } } }
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); }
void Write_RTC(void){ /* Syncin MOD-RTC with system clock */ time_t ltime; struct tm *rtc_tm; unsigned char buff[10]; int fd; ltime = time(NULL); rtc_tm = localtime(<ime); buff[0] = 0x02; //Pointer to the first register buff[1] = InttoBCD(rtc_tm -> tm_sec); buff[2] = InttoBCD(rtc_tm -> tm_min); buff[3] = InttoBCD(rtc_tm -> tm_hour); buff[4] = InttoBCD(rtc_tm -> tm_mday); buff[5] = InttoBCD(rtc_tm -> tm_wday); buff[6] = InttoBCD(rtc_tm -> tm_mon+1); //because local time counts months 0-11 but MOD-RTC 1-12 buff[7] = InttoBCD(rtc_tm -> tm_year); I2C_Open(&fd, 0x51); I2C_Send(&fd, buff, 8); I2C_Close(&fd); }
void MasterControl(void) { /* Master Store data */ TIDMstUpdateDevState(); /* Master recheck device */ if(TIDMstFirstInitFIN==1) { if(TMR1TimerCounter > 10) { if(I2CMstEndFlag==1) { TIDMstInitFIN=0; TMR1TimerCounter=0; I2C_Close(I2C_MS_PORT); I2C_MS_Master_Restart(); } } else { TIDMstInitFIN = 1; TMR1TimerCounter++; } } GetBattery(); }
//-------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------- int main(int argc, char **argv) { uint32 Counter; int16 Temperature; uint8 Humidity; uint8 HwRev; char Mode; Counter = 0; Mode = 0; HwRev = GetRaspberryHwRevision(); if((argc > 1) && ((argv[1][0]=='S') || (argv[1][0]=='L') || (argv[1][0]=='C'))) Mode = argv[1][0]; if(!Mode) { printf("Raspi-SHT21 V3.0.0 by Martin Steppuhn (www.emsystech.de) [" __DATE__ " " __TIME__"]\n"); printf("Options:\n"); printf(" S : [20.0 99]\n"); printf(" L : [temperature=20.0][humidity=99]\n"); printf(" C : [Temperature,20,0][Humidity,99]\n"); printf("RaspberryHwRevision=%i\r\n",HwRev); } if(HwRev < 2) I2C_Open("/dev/i2c-0"); // Hardware Revision 1.0 else I2C_Open("/dev/i2c-1"); // Hardware Revision 2.0 I2C_Setup(I2C_SLAVE, 0x40); if(I2cError) { I2C_PrintError(); exit(1); } SHT21_Read(&Temperature,&Humidity); if(Sht21Error == 0) { if( Mode == 'S') printf("%.1f\t%u\n",((float)Temperature)/10,Humidity); else if(Mode == 'L') printf("temperature=%.1f\nhumidity=%u\n",((float)Temperature)/10,Humidity); else if(Mode == 'C') printf("Temperature,%.1f\nHumidity,%u\n",((float)Temperature)/10,Humidity); else { while(1) { SHT21_Read(&Temperature,&Humidity); if(Sht21Error == 0) printf("%lu\t%.1f\t%u\n",Counter++,((float)Temperature)/10,Humidity); else { PrintSht21Error(); I2cError = 0; } DelayMs(1000); } } } else { PrintSht21Error(); } I2C_Close(); return(0); }
uint16_t IO2::analogRead(uint8_t pin) { int fd; uint16_t result; unsigned char buff[1] = {pin}, data[2]; I2C_Open(&fd, address); I2C_Send(&fd, buff, 1); I2C_Close(&fd); I2C_Open(&fd, address); I2C_Read(&fd, data, 2); I2C_Close(&fd); result = (data[1] << 8) | data[0]; return result; }
void IO2::setAddress(uint8_t newAddress) { int fd; unsigned char buff[2] = {SET_ADDRESS, newAddress}; I2C_Open(&fd, address); I2C_Send(&fd, buff, 2); I2C_Close(&fd); }
void I2C0_Close(void) { /* Disable I2C0 interrupt and clear corresponding NVIC bit */ I2C_DisableInt(I2C0); NVIC_DisableIRQ(I2C0_IRQn); /* Disable I2C0 and close I2C0 clock */ I2C_Close(I2C0); CLK_DisableModuleClock(I2C0_MODULE); }
int main(int argc, char **argv) { float temp = 0; I2C_Initialize(MCP79410_ADDRESS); //Initialize I2C and setup chip address RTCC_Struct *current_time = (RTCC_Struct *) malloc(sizeof(RTCC_Struct)); RTCC_Struct *alarm_time = (RTCC_Struct *) malloc(sizeof(RTCC_Struct)); RTCC_Struct user_alarm = {2, 57, 0, 0, 0, 0, 0}; //Change minutes value for match alarm_time = &user_alarm; //MCP79410_Initialize(); //Initialize RTCC with system time and date MCP79410_ClearInterruptFlag(ZERO); MCP79410_SetAlarmTime(alarm_time,ZERO); //Set alarm time MCP79410_SetAlarmMatch(MINUTES_MATCH,ZERO); //Alarm ZERO will trigger on minutes match MCP79410_SetAlarmMFPPolarity(LOWPOL,ZERO); //Configure Alarm pin polarity as HIGH MCP79410_SetMFP_Functionality(ALARM_INTERRUPT); //Set alaram interrupt PinLevel_t pval = ReadPinStatus(MPL_PIN); printf("MPL pin level is %d\r\n",(unsigned char)pval); pinModeOutput(LED_PIN); //Set LED pin as output while(1) { AlarmStatus_t alrm_flag = MCP79410_GetAlarmStatus(ZERO); //Check alarm status printf("Alarm interrupt status %d\r\n",(unsigned int)alrm_flag); if(alrm_flag != 0) { digitalWrite(LED_PIN, HIGH); //Let there be light delay_ms(500); I2C_Initialize(MPL3115A2_ADDRESS); //Initialize I2C peripheral delay_ms(500); MPL3115A2_Initialize(); //Initialize the sensor delay_ms(500); temp = MPL3115A2_ReadTemperature(); //Take a temperature reading temp = MPL3115A2_ReadTemperature(); printf("Temperature : %0.2f degree Celsius.\r\n", temp); delay_ms(500); I2C_Initialize(MCP79410_ADDRESS); delay_ms(500); alrm_flag = 0; //Reset alarm flag for next alarm MCP79410_ClearInterruptFlag(ZERO); delay_ms(1000); digitalWrite(LED_PIN, LOW); //Back to waiting time printf("Alarm :%d\r\n",(unsigned int)alrm_flag); } current_time = MCP79410_GetTime(); printf("now: %d-%d-%d %d:%d:%d\n", current_time->year, current_time->month, current_time->date, current_time->hour, current_time->min, current_time->sec); delay_ms(1000); } I2C_Close(); //Return I2C pins to default status return 0; }
int main(int argc, char **argv) { int file; unsigned char buffer[2], data[2], address; if(argc < 3) { printf("Too few arguments.\n Type -help.\n"); exit(1); } if(!strcmp(argv[1], "-relay")) { address = strtol(argv[2], NULL, 0); buffer[0] = 0x10; buffer[1] = strtol(argv[3], NULL, 0); I2C_Open(&file, address); I2C_Send(&file, buffer, 2); I2C_Close(&file); } else if(!strcmp(argv[1], "-dig")) { int i; address = strtol(argv[2], NULL, 0); buffer[0] = 0x20; I2C_Open(&file, address); I2C_Send(&file, buffer, 1); I2C_Read(&file, data, 1); I2C_Close(&file); for(i = 0; i < 4; i++) { printf("IN[%d] = %d\n", i+1, (data[0] >> i) & 0x01); } }
void IO2::setPullpin(uint8_t pin, uint8_t state) { if(state) pp_status |= pin; else pp_status &= ~pin; int fd; unsigned char buff[2] = {SET_PU, pp_status}; I2C_Open(&fd, address); I2C_Send(&fd, buff, 2); I2C_Close(&fd); }
void IO2::pinMode(uint8_t pin, uint8_t mode) { if(mode) tris_status |= pin; else tris_status &= ~pin; int fd; unsigned char buff[2] = {SET_TRIS, tris_status}; I2C_Open(&fd, address); I2C_Send(&fd, buff, 2); I2C_Close(&fd); }
void IO2::digitalWrite(uint8_t pin, uint8_t level) { if(level) lat_status |= pin; else lat_status &= ~pin; int fd; unsigned char buff[2] = {SET_LAT, lat_status}; I2C_Open(&fd, address); I2C_Send(&fd, buff, 2); I2C_Close(&fd); }
void IO2::setRelay(uint8_t relay, uint8_t state) { if(state) relay_status |= relay; else relay_status &= ~relay; int fd; unsigned char buff[2] = {SET_RELAY, relay_status}; I2C_Open(&fd, address); I2C_Send(&fd, buff, 2); I2C_Close(&fd); }
/* Read GPIO Analog Inputs */ void ReadADC(unsigned char value) { int fd; unsigned char buff[5]; unsigned char data[5]; switch(value) { case 255: ReadADC(0); ReadADC(1); ReadADC(2); ReadADC(3); ReadADC(5); break; case 0: case 1: case 2: case 3: case 5: buff[0]=0x10+value; data[0]=0x00; /* Open I2C-BUS */ I2C_Open(&fd, 0x21); /* Write register */ I2C_Send(&fd, buff,1 ); /* Read the ADC */ I2C_Read(&fd, data, 2); /* Convert to Volts. Vref = 3.3V, ADC is 10 bits */ float volts = data[0] * 0.003222656 + data[1]*0.825; printf("ADC%u: %1.3fV\n",value, volts); /* Close I2C-BUS */ I2C_Close(&fd); break; default: printf("ADC%u not found\n",value); } }
void Sync_RTC(void){ time_t ltime; struct tm *rtc_tm; ltime = time(NULL); rtc_tm = localtime(<ime); int fd; unsigned char buff[10]; unsigned char data[10]; buff[0] = 0x02; /* Open I2C-BUS */ I2C_Open(&fd, 0x51); /* Write register */ I2C_Send(&fd, buff, 1) ; /* Read the RTC */ I2C_Read(&fd, data, 7); /* Close I2C-BUS */ I2C_Close(&fd); data[0] &= 0x7F; data[1] &= 0x7F; data[2] &= 0x3F; data[3] &= 0x3F; data[4] &= 0x07; data[5] &= 0x1F; data[6] &= 0xFF; rtc_tm -> tm_sec = BCDtoInt(data[0]); rtc_tm -> tm_min = BCDtoInt(data[1]); rtc_tm -> tm_hour = BCDtoInt(data[2]); rtc_tm -> tm_mday = BCDtoInt(data[3]); rtc_tm -> tm_wday = BCDtoInt(data[4]); rtc_tm -> tm_mon = BCDtoInt(data[5]); rtc_tm -> tm_year = BCDtoInt(data[6]); const struct timeval tv = {mktime(rtc_tm), 0}; settimeofday(&tv, 0); }
/* Set GPIO Outputs */ void Set_LAT(unsigned char value){ int fd; unsigned char buff[5]; buff[0]=0x02; buff[1]=value; /* Open I2C-BUS */ I2C_Open(&fd, 0x21); /* Write register */ I2C_Send(&fd, buff,2 ); /* Close I2C-BUS */ I2C_Close(&fd); }
void Read_RTC(void){ int fd; unsigned char buff[10]; unsigned char data[10]; buff[0] = 0x02; /* Open I2C-BUS */ I2C_Open(&fd, 0x51); /* Write register */ I2C_Send(&fd, buff, 1) ; /* Read the RTC */ I2C_Read(&fd, data, 7); /* Close I2C-BUS */ I2C_Close(&fd); data[0] &= 0x7F; data[1] &= 0x7F; data[2] &= 0x3F; data[3] &= 0x3F; data[4] &= 0x07; data[5] &= 0x1F; data[6] &= 0xFF; printf("Sec: %d\n",BCDtoInt(data[0])); printf("Min: %d\n",BCDtoInt(data[1])); printf("Hour: %d\n",BCDtoInt(data[2])); printf("MDays: %d\n",BCDtoInt(data[3])); printf("WDays: %d\n",BCDtoInt(data[4])); printf("Month: %d\n",BCDtoInt(data[5])); printf("Year: %d\n",BCDtoInt(data[6])); }
/* Set New I2C device address */ void Set_Address(unsigned char value){ int fd; unsigned char buff[5]; buff[0]=0xF0; buff[1]=value; /* value: * New Address to be setted * / /* Open I2C-BUS */ I2C_Open(&fd, 0x21); /* Write register */ I2C_Send(&fd, buff,2 ); /* Close I2C-BUS */ I2C_Close(&fd); }
/* Switch Relay On */ void RelayOn(unsigned char value){ int fd; unsigned char buff[5]; buff[0]=0x41; buff[1]=value; /* value: * 1 - switch relay 1 on * 2 - switch relay 2 on * 3 - switch both relays on * / /* Open I2C-BUS */ I2C_Open(&fd, 0x21); /* Write register */ I2C_Send(&fd, buff,2 ); /* Close I2C-BUS */ I2C_Close(&fd); }