/******************************************************************************* * PUBLIC FUNCTION: delay_ms * * PARAMETERS: * ~ ui_millisecond * * RETURN: * ~ void * * DESCRIPTIONS: * delay for for period millisecond base on ui_millisecond * based on _XTAL_FREQ value, ui_millisecond range from 0-65,535. *******************************************************************************/ void delay_ms(unsigned int ui_millisecond) { for( ; ui_millisecond > 0; ui_millisecond --) { __delay_ms(1); } }
// ============================================================================================================ // This function uses the length given by PHOTO functions to receive the corresponding data of the picture // Arguments :int length (length of the photo) // Return :None // ============================================================================================================ void cam_recev_photo(unsigned int length){ unsigned int l,m; unsigned int numb = length/8; // Number of blocks of eight bytes each if((length % 8) > 0) numb++; // If lefts some bytes (lees than eigth), add one to variable numb char picture; char ret[10]; // Variable for the received data printf("\r\n"); for(l=0; l<numb; l++){ // Variable l goes from 0 to block number (numb) SPI_nSS_1=0; // Select the camera (NOTE: COULD BE BEFORE THE FOR) for(m=0; m<8; m++){ // Read eigth bytes each time picture = SPI_1_transfer(0x00); // Send a dummy byte to read one from the camera //utoa(ret, (unsigned int)(0x00FF&picture), 16); if( picture==0x00 ){ printf("00"); } else{ if( (picture&0xF0)==0x00 ){ printf("0"); sprintf (ret, "%X", ((unsigned char)picture) ); printf(ret); } else{ sprintf (ret, "%X", ((unsigned char)picture) ); printf(ret); } } } SPI_nSS_1=1; // Deselect the camera __delay_ms(12); // Delay of 12 us (Camera requierement) } printf("\r\n"); }
void main(){ int a = -12345; //初期設定 OSCCON = 0x72; // 内部クロックは8MHz ANSELA = 0x00; // すべてデジタルに割り当て TRISA = 0x20; // ピンは全て出力に割り当て PORTA = 0x00; // 出力ピン初期化 ANSELB = 0x00; // すべてデジタルに割り当て TRISB = 0x00; // ピンは全て出力に割り当て PORTB = 0x00; // 出力ピン初期化 // USART機能の設定を行う //RXDTSEL設定…… =0ならRB5を使う。=1ならRC5を使う。 //TXCKSEL設定…… =0ならRB7を使う。=1ならRC4を使う。 RXDTSEL = 1; // 5番ピン(RC5)をRX受信ピンとする TXCKSEL = 1; // 6番ピン(RC4)をTX送信ピンとする TXSTA = 0b00100100; // 送信情報設定:非同期モード 8ビット・ノンパリティ RCSTA = 0b10010000; // 受信情報設定 SPBRG = 51; // ボーレートを9600(高速モード)に設定 __delay_ms(500); // 5秒後に開始する while(1) { sousin(a); } }
/******************************************************************************* * PRIVATE FUNCTION: send_lcd_data * * PARAMETERS: * ~ b_rs - The output of the LCD RS pin (1 or 0). * ~ uc_data - The output of the LCD data bus. * * RETURN: * ~ void * * DESCRIPTIONS: * Set the output of the LCD RS pin and data bus. * *******************************************************************************/ void send_lcd_data(unsigned char b_rs, unsigned char uc_data) { // 8-bit Mode only need to send the data once. set_lcd_rs(b_rs); set_lcd_data(uc_data); // Send a positive e pulse. set_lcd_e(1); __delay_ms(1); set_lcd_e(0); __delay_ms(2); // make the default stage to logic low set_lcd_rs(0); set_lcd_data(0); }
/** * Soft I2C library initialization */ void I2C_Init() { I2C_SDA_OUT_SET(); I2C_SCL_OUT_SET(); I2C_SCL_SET(); I2C_SDA_SET(); __delay_ms(10); }
int main (void) { TRISA = 0b00000001; TRISB = 0b00000000; CMCON = 0b00000111; //cut off analog comparator just add this one PORTB = 0b00000000; int button = 0; //puch button counter while(1) { seg_light(button); if(RA0 == 0) { button ++; __delay_ms(200); if(button >= 10) { button = button - 10; } } } return 0; }
void handleFwWrite(rfTrcvPacket_t* packet) { uint16_t address = (packet->packet.data[2] << 8) | packet->packet.data[3]; uint16_t* packetData = (uint16_t*) &(packet->packet.data[8]); address=0; // TODO: Handle encryption eepromTxBytes(address, packet->packet.data+8, 8); // Wait 5ms for EEPROM write cycle __delay_ms(5); eepromRxBytes(address, packet->packet.data+8, 8); // use address for checksum calculation address = 0xCAFE; for (uint8_t i = 0; i < 4; i++) { address = address ^ (packetData[i] ^ 0x5AA5); } packet->packet.data[8] = address >> 8; packet->packet.data[9] = address & 0xFF; packet->packet.size = 10; }
int main(void) { init(); ei(); unsigned char decoder_address, alarms; while(1) { unsigned char i; for (i=0; i<4; i++) { decoder_address = (PORTA & ~0x03) | (i & 0x03); alarms = (PORTC & ~0x3C) | (alarms_state[i] & 0x3C); // If blinking and lights must be turned off if (light == 0) { if (i == 0) alarms &= 0b00011100; if (i == 1) alarms &= 0b00111000; } select_disassert(); PORTA = decoder_address; PORTC = alarms; select_assert(); __delay_ms(2); } } }
/** Prototype: TODO fill this in Input: Output: none Description: Usage: charger_init(); */ void charger_init(void) { // uint8_t temp; //reset all registers REG01 b7->1 then wait a few ms i2c_slave_command(CHRGR_ADDR, 0x01, 0x80); __delay_ms(5); // let it reset // //REG00 disable hi-z; reduce dynamic power mgmt VINDPM (min) to 4.5 to allow for cable v-drop; Set INLIM (max) to 2A, up to 3 if connectors can take it i2c_slave_command(CHRGR_ADDR, 0x00, 0x40); // //REG01 reset watchdog; set charger config as OTG enable; batt charger enable; set Vsys,min = 3.5 i2c_slave_command(CHRGR_ADDR, 0x01, 0x5B); // //REG02 set ICHG (max) to 2A; set boost tmprtr mon BCOLD to -10C; don't force ICHG*0.20 i2c_slave_command(CHRGR_ADDR, 0x02, 0x60); // //REG03 set precharge current IPRECHG to 256mA; set Termination current ITERM to 256mA i2c_slave_command(CHRGR_ADDR, 0x03, 0x11); // //REG04 set charge Vlim VREG to 4.2V; set BATLOWV (precharge threshold) to 3.0V; set battery recharge threshold VRECHG to 0.1V i2c_slave_command(CHRGR_ADDR, 0x04, 0xB2); // //REG05 Enable charging terination; set 40s watchdog period; enable charging safety timer; set CHG_TIMER for 12 hrs i2c_slave_command(CHRGR_ADDR, 0x05, 0x8A); //TODO re-enable watchdog timer // //REG06 set BOOSTV to 5.1V; disable boost thermal protection (change if thermistor involved); set thermal regulation to 120C (ICHG reduced) i2c_slave_command(CHRGR_ADDR, 0x06, 0x9F); // //REG07 safety timer slowed by 2X during DPM; allow BATFET; INT on CHRG_FLT; INT on BAT_FLT i2c_slave_command(CHRGR_ADDR, 0x07, 0xCB); // force DPDM connect; return; }
void InitApp(void) { // activation de la priorité des interruptions _NSTDIS = 0; //Init des E/S _TRISA0 = 0; _TRISA1 = 0; led = 0; led1 = 0; OpenTimer2(T2_ON & T2_GATE_OFF & T2_PS_1_256 & T2_32BIT_MODE_OFF & T2_SOURCE_INT, 50000); ConfigIntTimer2(T2_INT_PRIOR_2 & T2_INT_ON); //Init debug on UART, TX->RP8, RX->RP9 InitDebug(8,9); //Init the Servo Lib InitLibServo(); ajouterServo('B',2); //Initialise RB2, pas obligatoire ajouterServo('B',3); //Initialise RB3, pas obligatoire __delay_ms(100); modifierServoPeriod('B',2,0.0015); //1.5ms, milieu modifierServoPeriod('B',3,0.002); //2ms, droite }
/******************************************************************************* * PRIVATE FUNCTION: delay_ms * * PARAMETERS: * ~ ui_value - The period for the delay in miliseconds. * * RETURN: * ~ void * * DESCRIPTIONS: * Delay in miliseconds. * *******************************************************************************/ void delay_ms(unsigned int ui_value) { while (ui_value-- > 0) { __delay_ms(1); // macro from HI-TECH compiler which will generate 1ms delay base on value of _XTAL_FREQ in system.h } }
/******************************************************************************* * PUBLIC FUNCTION: Keypad_getKey * * PARAMETERS: * ~ void * * RETURN: * ~ unsigned char - Return pressed KEY * * DESCRIPTIONS: * Returns the key that is pressed, if any. This function is non-blocking. * *******************************************************************************/ unsigned char Keypad_getKey() { unsigned char Key = 0; if (KP_COL1==1 && KP_COL2==1 && KP_COL3==1 && KP_COL4==1) { return Key; } else { // Start Row Scaning for (unsigned char row=1; row<=ROWS; row++) { Select_ROW(row); Key = findKey_inRow(row); if (Key) break; // Break the scan if any Key found } // Back to normal KP_ROW1 = LOW; KP_ROW2 = LOW; KP_ROW3 = LOW; KP_ROW4 = LOW; __delay_ms(KEYHOLD_DELAY); //Apply a Typematic/Hold delay return Key; } }
void main(void) { char s[] = "Hello!"; TRISA = 0x00; PORTA = 0x00; TRISB = 0x00; PORTB = 0x00; /**************** * ANSEL ****************/ ANSEL = 0x00; // sd1602_init(); // // sd1602_clear(); while(1) { //sd1602_clear(); // sd160b2_print(s); __delay_ms(1000); } // PORTA = 0x00; // // __delay_ms(200); return; }
void Calibrate_Gyros() { int x = 0; GYRO_XOUT_OFFSET_1000SUM = 0; GYRO_YOUT_OFFSET_1000SUM = 0; GYRO_ZOUT_OFFSET_1000SUM = 0; for(x = 0; x<5000; x++) { LDByteReadI2C(MPU6050_ADDRESS, MPU6050_RA_GYRO_XOUT_H, &GYRO_XOUT_H, 1); LDByteReadI2C(MPU6050_ADDRESS, MPU6050_RA_GYRO_XOUT_L, &GYRO_XOUT_L, 1); LDByteReadI2C(MPU6050_ADDRESS, MPU6050_RA_GYRO_YOUT_H, &GYRO_YOUT_H, 1); LDByteReadI2C(MPU6050_ADDRESS, MPU6050_RA_GYRO_YOUT_L, &GYRO_YOUT_L, 1); LDByteReadI2C(MPU6050_ADDRESS, MPU6050_RA_GYRO_ZOUT_H, &GYRO_ZOUT_H, 1); LDByteReadI2C(MPU6050_ADDRESS, MPU6050_RA_GYRO_ZOUT_L, &GYRO_ZOUT_L, 1); GYRO_XOUT_OFFSET_1000SUM += ((GYRO_XOUT_H<<8)|GYRO_XOUT_L); GYRO_YOUT_OFFSET_1000SUM += ((GYRO_YOUT_H<<8)|GYRO_YOUT_L); GYRO_ZOUT_OFFSET_1000SUM += ((GYRO_ZOUT_H<<8)|GYRO_ZOUT_L); __delay_ms(1); } GYRO_XOUT_OFFSET = GYRO_XOUT_OFFSET_1000SUM/5000; GYRO_YOUT_OFFSET = GYRO_YOUT_OFFSET_1000SUM/5000; GYRO_ZOUT_OFFSET = GYRO_ZOUT_OFFSET_1000SUM/5000; printf("\nGyro X offset sum: %ld Gyro X offset: %d", GYRO_XOUT_OFFSET_1000SUM, GYRO_XOUT_OFFSET); printf("\nGyro Y offset sum: %ld Gyro Y offset: %d", GYRO_YOUT_OFFSET_1000SUM, GYRO_YOUT_OFFSET); printf("\nGyro Z offset sum: %ld Gyro Z offset: %d", GYRO_ZOUT_OFFSET_1000SUM, GYRO_ZOUT_OFFSET); }
void main(void) { //Initialize the LCD module LCDInit(LS_NONE); //Initialize the ADC module ADCInit(); //Clear the LCD LCDClear(); LCDWriteString("Thermometer"); while(1) { //Read the temperature using LM35 float t = LM35ReadTemp(); //Print it on the LCD LCDWriteIntXY(0,1,t,3); //Print the degree symbol and C LCDWriteString("%0C"); //Wait 200ms before taking next reading __delay_ms(200); } }
/** Prototype: TODO fill this in Input: Output: none Description: Usage: charger_init(); */ void usb_port_init(uint8_t addr) { // send a dummy read command to wake up the chip before doing anyhting. wait 3ms i2c_slave_read(addr, 0x00); //clear the junk byte __delay_ms(3); //TODO reset all registers then wait a few ms // TODO use GUI to go through and set all registers to what it's doing // ***Config registers // REG15 discharge VBUS and reset charge ration count i2c_slave_command(addr, 0x15, 0x13); // REG15 The ALERT# pin will be asserted if an error condition or indicator event is detected, The ALERT# pin will not be asserted on LOW_CUR or TREG, disable discharge and close VBUS switch, disable rationing, set ration to ignore i2c_slave_command(addr, 0x15, 0x03); // REG16 (default)The timeout and idle reset functionality is disabled for I2C, Emulation timeout is enabled during the DCE Cycle, restart DCE Cycle if no profile accepted, keep last DPDM setting once device starts drawing >Ichg/profile accepted, i2c_slave_command(addr, 0x16, 0x8C); // REG17 set pin ignore, M1=0,M2=EM_EN=1 (BC1.2 DCP), S0=1 (detection enabled), auto-retry on fault/error condition i2c_slave_command(addr, 0x17, 0xF6); // REG18 (default) set discharge time to 200ms, set attach/removal detection threshold to 800uA/700uA i2c_slave_command(addr, 0x18, 0x46); // REG25 disable highspeed data switch in all cases (it's not connected to anything) i2c_slave_command(addr, 0x25, 0x10); // *** End Config registers // REG19 set current limit to 2.5A (matches ILIM set by external resistor) i2c_slave_command(addr, 0x19, 0x03); //REG1E (default) set IBUS_CHG (min acceptable current when cycling DCE profiles) to 39mA i2c_slave_command(addr, 0x1E, 0x04); // REG1A-1B set max current ration to pack capacity BATT_CAP TODO adjust this smartly between ports based on how much has been drawn i2c_slave_command(addr, 0x1A, 0x0F); //REG20 disable DCP during DCE cycling i2c_slave_command(addr, 0x20, 0x16); i2c_slave_command(addr, 0x10, 0x00); // clear any existing error flags return; }
int main(void) { init(); int thrust = 0; // Holds thrust %age int old_thrust = 0; // Holds old thrust value. Used to limit the number of bytes sent int checksum = 0; // checksum for outgoing serial data. Integrity is important! while(1) { if (unlock) { // If the safety switch is active LATBbits.LATB5 = 1; // Green LED thrust = get_thrust(old_thrust); clear_bt_buf(); __delay_ms(5); } else { // System is locked! LATBbits.LATB5 = 0; // Red LED clear_bt_buf(); thrust = 0; } if (thrust != old_thrust) { old_thrust = thrust; uart_putChar('t', 2); // t for thrust, same as amarino uart_putChar(thrust, 2); checksum = ('t' ^ thrust); // Checksum is pretty simple for two bytes! uart_putChar(checksum, 2); } } return 0; }
void _Loop(void){ while(1) { //REF binary http://www.microchip.com/forums/m504764.aspx PORTB = 0x01; __delay_ms(500); PORTB = 0x02; __delay_ms(500); } }//void _Loop(void)
void Sound_Play(int _freq, int t) { PTPER = _freq; PDC1 = _freq; __delay_ms(t); }
void pcd8544_reset() { uint8_t i; // Issuing command LATCbits.LC1 = 0; LATCbits.LC0 = 0; for(i=0;i<8;i++){ __delay_ms(25); } //__delay_ms(25); //__delay_ms(25); //__delay_ms(25); //__delay_ms(25); //__delay_ms(25); //__delay_ms(25); LATCbits.LC0 = 1; //__delay_ms(25); //__delay_ms(25); // Device deselect LATCbits.LC1 = 1; }
void lcd_indicate(char test) { lcd_goto(0x0D); __delay_us(50); lcd_putch(test); __delay_ms(500); }
void Init_Ultrasons (void) { PIN_CN_ULTRASON_AR_IE = 0; // desactivation de l'IT PIN_CN_ULTRASON_AV_IE = 0; // desactivation de l'IT TRIS_ULTRASON_AV = 1; // mise en input de la pin TRIS_ULTRASON_AR = 1; // mise en input de la pin CloseTimer4(); __delay_ms(5); // attente de 5 ms pour bien tuer l'IT Etat_Ultrason = U_ETAT_OFF; nb_Coups_Timers = 0; Ultrason_AV_Detect = 0; Ultrason_AR_Detect = 0; Threshold_US = ULTRASON_THRESOLD; OpenTimer4(T4_ON & T4_GATE_OFF & T4_PS_1_8 & T4_SOURCE_INT, 0xFFFF ); // FCY = 40Meg prescaler à 8 donc F timer = 5Meg // 1 coup = 200 ns = 0.2us // max = 65535 => 13.107 ms // ce qui correspondra à de l'overshoot // configuration des interruptions ConfigIntTimer4(T4_INT_PRIOR_2 & T4_INT_ON); Init_CN(); Etat_Ultrason = U_ETAT_FOR_SEND1; }
int main() { const float enYuksekSicaklik = 27.0; const float katsayi = 5000.0 / 1024.0; float milivolt, santigrat; unsigned int sicaklik; TRISB = 0xff; TRISA = 1; TRISC = 0; nRBPU = 0; ADCON1 = 0x8e; // sadece AN0 analog diğer kanallar dijital ADCON0 = 0x41; // AN0 analog kanalını ve saat pulsunu seç for( ; ;) { ADCON0 = 0x45; // çevrime başla while ((ADCON0 & 4) != 0); // çevrim bitene kadar bekle sicaklik = ADRESH; sicaklik = sicaklik * 256 + ADRESL; milivolt = sicaklik * katsayi; santigrat = milivolt / 10.0; if (santigrat > PORTB) alarm = 1; else alarm = 0; __delay_ms(250); } }
void main(void) { int count; unsigned int adcValue = 0; unsigned int adcValueOld = 0; char buffer[16]; ConfigureOscillator(); InitApp(); lcd_init(); lcd_enable(); lcd_cursor_off(); lcd_test(); lcd_pos(2, 1); while (1) { ConvertADC(); while (BusyADC()); adcValue = ReadADC(); if (adcValue != adcValueOld) { lcd_clear(); lcd_pos(1, 1); memset(&buffer[0], 0, sizeof(buffer)); sprintf(&buffer[0], "Valor: %.4u %.3u%%", adcValue, (int)(((float)adcValue / 1024) * 100)); lcd_write_buffer(buffer, strlen(buffer)); adcValueOld = adcValue; } __delay_ms(20); } }
void main(void) { //initialize portc PORTC = 0x00; TRISC = 0x00; //initialize pwm PWM_Init(5000); //initialize pwm at 5khz PWM1_Duty(0); //set initial duty cycle at 0 on pin RC2 PWM2_Duty(0); //start pwm PWM1_Start(); PWM2_Start(); while (1) { for (int i=0; i<1023; i+=4) { PWM1_duty(); PWM2_Duty(1023-i); __delay_ms(2); } } }
void main(void) { /* Inicializacion de los perifericos I/O para la aplicacion */ TRISA = 0; // Todos los pines son configurados como salida ADCON1 = 0x06; // Todos los pines I/O son configurados como digitales (pag. 112 del manual) PORTA = 0b000001; // Inicializacion del puerto A __delay_ms(1000); while(1) { if (PORTA >= 0b100000) { PORTA = 0b000001; } else { PORTA <<= 1; } __delay_ms(1000); } }
// Initialize TRISX registers and set oscillator frequency void init_pic() { // Disable all analog inputs ANSELA = 0; ANSELB = 0; // Initialize audio pins LATBbits.LATB2 = 1; LATBbits.LATB3 = 1; PORTBbits.RB2 = 1; PORTBbits.RB3 = 1; // Enable internal pull-up on audio pins WPUB2 = 1; WPUB3 = 1; // Configure for 32MHz operation with internal oscillator OSCCON |= 0b11111000; // Wait a tenth of a second for clock to stabilize __delay_ms(100); // Configure RB2 and RB3 (audio triggers) as outputs TRISB &= 0b11110011; RB2 = 1; RB3 = 1; // Reset the software if necessary to prevent sounds playing on startup if(RB2 == 0 || RB3 == 0) { asm("RESET"); } // Enable global interrupts GIE = 1; }
void junction_follow(void){ signed char lineposition=0; unsigned char i=0; unsigned char status=0; lcd_clr(); lcd_goto(0); lcd_putstr("Junction\nCount"); __delay_ms(500); lcd_clr(); while(1){ do{ status=LSA08_ClearJunction(); }while(status==0); jfolo(2); RotateRight(); motor(180,180); __delay_ms(500); do{ lineposition=LSA08_GetPosition(); LED1=1; }while(lineposition>20); Brake(); LED1=0; __delay_ms(100); RotateRight(); motor(180,180); __delay_ms(500); do{ LED1=1; lineposition=LSA08_GetPosition(); }while(lineposition>20); Brake(); LED1=0; __delay_ms(100); } }
// TODO: Get this thing to be able to setup ANY of the UART channels somehow!! void config_uart(unsigned long baudRate) { // Make sure our buffers start empty: reset_buffer(); // Configure RX pin: // Set R7 as an input. I don't think this is needed for uart.. but let's // just be sure. //TRISBBITS rxPinU1 = TRISBbits.TRISB7; TRISBbits.TRISB7 = 1; OSCCONbits.IOLOCK = 0; // unlock the peripheral Control Register Lock // Configure RX pin: RPINR18bits.U1RXR = 7; // Map UART1 RX peripheral to RP7 // RPINR18bits.U1RXR = 26; // Map UART1 RX peripheral to RP26 (also, RG7) // Configure TX pin: RPOR3bits.RP6R = 3; // Map RP6 pin to UART1 TX OSCCONbits.IOLOCK = 1; // relock the peripheral Control Register Lock // Set baud rate: U1BRG = (((FCY)/(4 * baudRate)) - 1); // Set interrupt priorities: // (Honestly, not sure exactly what this is doing...) IPC3bits.U1TXIP2 = 1; //Set Uart TX Interrupt Priority IPC3bits.U1TXIP1 = 0; IPC3bits.U1TXIP0 = 0; IPC2bits.U1RXIP2 = 1; //Set Uart RX Interrupt Priority IPC2bits.U1RXIP1 = 0; IPC2bits.U1RXIP0 = 0; U1TXREG = 0; // Ensure that we start with an empty TX buffer U1STA = 0; // Clear the UxSTA register (for tx1 and rx1 only) U1STAbits.URXISEL = 0; // Configure when the TX interrupt will fire // Enable UART for 8-bit data: U1MODE = 0; // Clear the UxMode register (for tx1 and rx1 only) //U1MODEbits.RTSMD = 1; // RTS pin bit is in simplex mode not flow control mode U1MODEbits.BRGH = 1; // 1 = low speed U1MODEbits.UARTEN = 1; // Actually enable the UART module (MUST be done before UTXEN is set) U1STAbits.UTXEN = 1; // enable transmit IEC0bits.U1TXIE = 1; // enable transmit interrupt IEC0bits.U1RXIE = 1; // enable receive interrupt // Clear the flags for both interrupts: IFS0bits.U1TXIF = 0; IFS0bits.U1RXIF = 0; // Configure the buffer pointers: U1TXCharPtr = &txBufU1[0]; U1RXCharPtr = &rxBufU1[0]; __delay_ms(100); printf("\n\nUART should be working at 115200 baud now!\r\n"); }
void Calibrate(void){ LSA08_Calibrate(); RotateRight(); motor(180,180); __delay_ms(600); RotateLeft(); __delay_ms(600);__delay_ms(600); RotateRight(); motor(180,180); __delay_ms(600); motor(0,0);Forward(); lcd_clr(); }