void lcd_send_nibble(int8 nibble) { // Note: !! converts an integer expression // to a boolean (1 or 0). output_bit(LCD_DB4, !!(nibble & 1)); output_bit(LCD_DB5, !!(nibble & 2)); output_bit(LCD_DB6, !!(nibble & 4)); output_bit(LCD_DB7, !!(nibble & 8)); delay_cycles(1); output_high(LCD_E); delay_us(2); output_low(LCD_E); }
/* * This routine is called to encode a symbol. The symbol is passed * in the SYMBOL structure as a low count, a high count, and a range, * instead of the more conventional probability ranges. The encoding * process takes two steps. First, the values of high and low are * updated to take into account the range restriction created by the * new symbol. Then, as many bits as possible are shifted out to * the output stream. Finally, high and low are stable again and * the routine returns. */ void ArithmeticUtilEncoder::encode_symbol( SYMBOL& s ) { long range; /* * These three lines rescale high and low for the new symbol. */ range = (long) ( high-low ) + 1; high = low + (unsigned int ) (( range * s.high_count ) / SCALE - 1 ); low = low + (unsigned int ) (( range * s.low_count ) / SCALE ); /* * This loop turns out new bits until high and low are far enough * apart to have stabilized. */ for ( ; ; ) { /* * If this test passes, it means that the MSDigits match, and can * be sent to the output stream. */ if ( ( high & 0x80000000 ) == ( low & 0x80000000 ) ) { output_bit( high & 0x80000000 ); while ( underflow_bits > 0 ) { output_bit( ~high & 0x80000000 ); underflow_bits--; } } /* * If this test passes, the numbers are in danger of underflow, because * the MSDigits don't match, and the 2nd digits are just one apart. */ else if ( ( low & 0x40000000 ) && !( high & 0x40000000 )) { underflow_bits += 1; low &= 0x3fffffff; high |= 0x40000000; } else return ; low <<= 1; low &= 0xffffffff; high <<= 1; high &= 0xffffffff; high |= 1; } }
void output_byte(long byte,int len) { int i; int mask; /* MSB first */ mask = 1<<(len-1); for(i=0;i<len;i++) { if(byte & mask) output_bit(1); else output_bit(0); mask >>= 1; } }
set_pot (int pot_num, int new_value) { byte i; byte cmd[3]; if (pot_num >= NUM_POTS) return; pots[pot_num] = new_value; cmd[0]=pots[0]; cmd[1]=pots[1]; cmd[2]=0; for(i=1;i<=7;i++) shift_left(cmd,3,0); output_high(RST1); delay_us(2); for(i=1;i<=17;i++) { output_bit(DI, shift_left(cmd,3,0)); delay_us(2); output_high(CLK); delay_us(2); if(i==17) output_low(RST1); output_low(CLK); delay_us(2); } }
void write_dac(int16 data) { BYTE cmd[3]; BYTE i; cmd[0]=data; cmd[1]=(data>>8); cmd[2]=0x03; output_high(DAC_LDAC); output_low(DAC_CLK); output_low(DAC_CS); for(i=0; i<=23; ++i) { if(i<4 || (i>7 && i<12)) shift_left(cmd,3,0); else { output_bit(DAC_DI, shift_left(cmd,3,0)); output_high(DAC_CLK); output_low(DAC_CLK); } } output_high(DAC_CS); output_low(DAC_LDAC); delay_us(10); output_HIGH(DAC_LDAC); }
void main(void) { //we use PIN_C2 as an event to determine if we should start the USB CDC //bootloader. if it is not low (button is not pressed) then goto the //application, else if is low (button is pressed) then do the bootloader. output_bit(PIN_C2, 1); if(!input(PIN_C2)) { setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H | SPI_CLK_DIV_16); //Max7219 Initialized initLedDriver(); g_InBootloader = TRUE; usb_cdc_init(); usb_init(); while(!usb_enumerated()); load_program(); } g_InBootloader = FALSE; #ASM goto APPLICATION_START #ENDASM }
byte READ_EXT_SRAM(byte Address) { byte Cnt,Data; EnableSRAM(TRUE); // Send Read Address // write: bit 7 = 0 Data = 0; // other bits are address Data |= Address; EnableSRAM(TRUE); for(Cnt = 8; Cnt > 0; Cnt--) { output_bit(SRAM_MOSI,bit_test(Data,(Cnt - 1))); output_high(SRAM_SCK); output_low(SRAM_SCK); } // Read each bit from address Data = 0; for(Cnt = 8; Cnt > 0; Cnt--) // shift in bits 7 - 0 { output_high(SRAM_SCK); output_low(SRAM_SCK); if(input(SRAM_MISO)) bit_set(Data,(Cnt - 1)); } EnableSRAM(FALSE); return(Data); }
//Ham Gui 4 Bit Du Lieu Ra LCD void LCD_Send4Bit( unsigned char Data ) { output_bit(LCD_D4,Data&0x01); output_bit(LCD_D5,(Data>>1)&1); output_bit(LCD_D6,(Data>>2)&1); output_bit(LCD_D7,(Data>>3)&1); }
main() { short output; long count,max_count; error_count=0; resync(); do { while((input_a() & 3)==sequence[seq_index]) ; seq_index=(seq_index+1)&3; if ((input_a() & 3)!=sequence[seq_index]) resync(); else { if(error_count==0) output_low(pin_a2); else --error_count; max_count=input_b()>>2; count++; if(count>max_count) { output=!output; output_bit(pin_a3,output); count=0; } } } while (true); }
void lcd_send_nibble(BYTE n) { #if (defined(LCD_DATA4) && defined(LCD_DATA5) && defined(LCD_DATA6) && defined(LCD_DATA7)) /* Write to the data port */ output_bit(LCD_DATA4, bit_test(n, 0)); output_bit(LCD_DATA5, bit_test(n, 1)); output_bit(LCD_DATA6, bit_test(n, 2)); output_bit(LCD_DATA7, bit_test(n, 3)); #else lcdlat.data = n; #endif delay_cycles(1); lcd_output_enable(1); delay_us(2); lcd_output_enable(0); }
void internalLogic() { output_bit(FEUX_STOP, feuxstop); // si feuxstop est à true, allumer les feux stop, et inversement output_bit(FEUX_ARR, feuxarr); // si feuxarr est à true, allumer les feux arrière, et inversement if(ms == 500) // 500 ms après l'activation du timer { ms = 0; // repartir pour 500 ms clign_on = !clign_on; // changer l'état d'allumage des clignotants output_bit(CLIGN_ARD, clign_on && clignd); // allumer ou éteindre le cligno droit s'il est activé output_bit(CLIGN_ARG, clign_on && cligng); // allumer ou éteindre le cligno gauche s'il est activé } }
/* * This routine is called to encode a symbol. The symbol is passed * in the SYMBOL structure as a low count, a high count, and a range, * instead of the more conventional probability ranges. The encoding * process takes two steps. First, the values of high and low are * updated to take into account the range restriction created by the * new symbol. Then, as many bits as possible are shifted out to * the output stream. Finally, high and low are stable again and * the routine returns. */ void encode_symbol( FILE *stream, SYMBOL *s ) { unsigned long long range; range = (unsigned long long) ( high-low ) + 1; high = SafeConvert(div128(mult128(range, s->high_count), s->scale).lo + low - 1); low = SafeConvert(div128(mult128(range, s->low_count), s->scale).lo + low); /* * This loop turns out new bits until high and low are far enough * apart to have stabilized. */ for ( ; ; ) { /* * If this test passes, it means that the MSDigits match, and can * be sent to the output stream. */ if ( ( high & LAST_BIT ) == ( low & LAST_BIT ) ) { output_bit( stream, high & LAST_BIT ); while ( underflow_bits > 0 ) { output_bit( stream, ~high & LAST_BIT ); underflow_bits--; } } /* * If this test passes, the numbers are in danger of underflow, because * the MSDigits don't match, and the 2nd digits are just one apart. */ else if ( ( low & NEXT_TO_LAST_BIT ) && !( high & NEXT_TO_LAST_BIT )) { underflow_bits += 1; low &= NEXT_TO_LAST_BIT_MINUS_ONE; high |= NEXT_TO_LAST_BIT; } else return ; low <<= 1; high <<= 1; high |= 1; } }
// When power failure occurs HT (halt update) is set to 1 // ... preventing the clock from updating registers // HT must be set to 0 to resume register updates void RTC_reset_HT() { int8 RTC_buffer; RTC_buffer = 0; setup_spi(SPI_MASTER|SPI_MODE_0_0|SPI_CLK_DIV_16); output_bit(RTC_CS, ENABLE); RTC_buffer = spi_read(0x0C); RTC_Al_Hr_Reg = spi_read(RTC_buffer); output_bit(RTC_CS, DISABLE); RTC_Al_Hr_Reg = RTC_Al_Hr_Reg & 0b10111111; output_bit(RTC_CS, ENABLE); RTC_buffer = spi_read(0x8C); // address - Hour RTC_buffer = spi_read(RTC_Al_Hr_Reg); // data output_bit(RTC_CS, DISABLE); }
// Enables IRQ output (hardware) void RTC_set_AFE() { int8 RTC_buffer; RTC_buffer = 0; setup_spi(SPI_MASTER|SPI_MODE_0_0|SPI_CLK_DIV_16); output_bit(RTC_CS, ENABLE); RTC_buffer = spi_read(0x0A); RTC_Al_Mon_Reg = spi_read(RTC_buffer); output_bit(RTC_CS, DISABLE); RTC_Al_Mon_Reg = RTC_Al_Mon_Reg | 0b10000000; output_bit(RTC_CS, ENABLE); RTC_buffer = spi_read(0x8A); // address - Month RTC_buffer = spi_read(RTC_Al_Mon_Reg); // data output_bit(RTC_CS, DISABLE); }
void lcd_envia_byte(boolean endereco,byte dado){ output_low(rs); output_bit(rs,endereco); delay_us(100); output_low(en); lcd_cmd(dado>>4); lcd_cmd(dado&0x0f); delay_ms(1); }
void write_adc_byte(byte data) { byte i; output_low(ADC_CS); for(i=1;i<=8;++i) { output_low(ADC_CLK); output_bit(ADC_DI, shift_left(&data,1,0)); output_high(ADC_CLK); } output_high(ADC_CS); }
// envia um dado de quatro bits para o display void lcd_envia_nibble(int dado) { // coloca os quatro bits nas saidas output_bit(lcd_d4, bit_test(dado, 0)); output_bit(lcd_d5, bit_test(dado, 1)); output_bit(lcd_d6, bit_test(dado, 2)); output_bit(lcd_d7, bit_test(dado, 3)); // dá um pulso na linha enable if(lcd_modulo == 0) { output_high(lcd_en); delay_us(2); output_low(lcd_en); #ifdef lcd_en2 output_high(lcd_en2); delay_us(2); output_low(lcd_en2); #endif } if(lcd_modulo == 1) { output_high(lcd_en); delay_us(2); output_low(lcd_en); } #ifdef lcd_en2 if(lcd_modulo == 2) { output_high(lcd_en2); delay_us(2); output_low(lcd_en2); } #endif }
// envia um dado de um byte para o display void lcd_envia_byte(short modo, int dado) { // configura a linha rs dependendo do modo selecionado output_bit(lcd_rs, modo); // envia a primeira parte do byte lcd_envia_nibble(dado >> 4); // envia a segunda parte do byte lcd_envia_nibble(dado & 0x0f); // aguarda 50 us delay_us(50); }
void onewire_write(int data) { int count; for (count=0; count<8; ++count) { output_low(ONE_WIRE_PIN); delay_us( 2 ); // pull 1-wire low to initiate write time-slot. output_bit(ONE_WIRE_PIN, shift_right(&data,1,0)); // set output bit on 1-wire delay_us( 60 ); // wait until end of write slot. output_float(ONE_WIRE_PIN); // set 1-wire high again, delay_us( 2 ); // for more than 1us minimum. } }
int16 mcp3208_read(int8 ch) { int16 value; int8 i; int8 c; output_low(MCP3208_CLK); output_high(MCP3208_DIN); output_low(MCP3208_NCS); if ( 0 == ch ) c=0b00011; else if ( 1 == ch ) c=0b10011; else if ( 2 == ch ) c=0b01011; else if ( 3 == ch ) c=0b11011; else if ( 4 == ch ) c=0b00111; else if ( 5 == ch ) c=0b10111; else if ( 6 == ch ) c=0b01111; else c=0b11111; /* select out channel and start the conversion */ for ( i=0 ; i<5 ; i++ ) { output_low(MCP3208_CLK); output_bit(MCP3208_DIN,c&1); c=c>>1; output_high(MCP3208_CLK); } value=0; for ( i=0 ; i<14 ; i++ ) { output_low(MCP3208_CLK); shift_left(&value,2,input(MCP3208_DOUT)); output_high(MCP3208_CLK); } bit_clear(value,13); bit_clear(value,12); output_high(MCP3208_NCS); return value; }
/*=========================================================================== || MAIN || =============================================================================*/ void main(void) { short execute = 0; setup_devices(); while(1){ if(_debug_usb()){ test_comunicacion(); test_memoria(); //test_ccp(); }else{ output_bit(INDICADOR_AMARILLO, execute); execute = !execute; delay_ms(333); } } }
void init() { unsigned int16 i; blink(); init_queue(&rxque0); init_queue(&txque0); set_step(1); // 0=full, 1=1/2, 5=1/4, 4=1/8, 7=1/16 enable(1); //setup_counters(T0_INTERNAL, T0_DIV_1 | T0_8_BIT); setup_counters(T0_INTERNAL, T0_DIV_2 | T0_8_BIT); set_timer0(0); enable_interrupts(INT_RTCC); enable_interrupts(INT_RDA); enable_interrupts(GLOBAL); output_bit(CTS,0); // ready to receive from usb output_bit(STROBE,1); // parallel port strobe // input(PIN_C3); // ESTOP input(PIN_C4); // ALIM input(PIN_C0); // XLIM input(PIN_C1); // YLIM // input(PIN_C2); // ZLIM output_bit(PIN_C2,0); // changed from ZLIM to PWM OUTPUT setup_ccp1(CCP_PWM); setup_timer_2(T2_DIV_BY_16, 255, 1); // mode, period, postscale set_timer2(0); set_pwm1_duty(0); // duty cycle is val/(4*(255+1)) }
void vfd_putc(char data) { int8 i; output_low(VFD_CLK); output_low(VFD_TX); while (input(VFD_BUSY)) { } delay_us(10); for (i=0;i<8;i++) { // disable_interrupts(global); output_bit(VFD_TX, shift_left(&data,1,0) ); delay_cycles(4); output_high(VFD_CLK); // enable_interrupts(global); delay_us(10); output_low(VFD_CLK); delay_us(10); } }
void READ_EXT_SRAM_STRING(byte Address, char* ptrString) { byte Cnt, Data; char* ptrStartString; ptrStartString = ptrString; if(!*ptrString) return; EnableSRAM(TRUE); // Send Read Address // write: bit 7 = 0 Data = 0; // other bits are address Data |= Address; EnableSRAM(TRUE); for(Cnt = 8; Cnt > 0; Cnt--) { output_bit(SRAM_MOSI,bit_test(Data,(Cnt - 1))); output_high(SRAM_SCK); output_low(SRAM_SCK); } // Read ptrString to SRAM do { Data = 0; for(Cnt = 8; Cnt > 0; Cnt--) // shift in bits 7 - 0 { output_high(SRAM_SCK); output_low(SRAM_SCK); if(input(SRAM_MISO)) bit_set(Data,(Cnt - 1)); } if(Data < 32 || Data > 126) // if not a keyboard char *ptrString = 0; *ptrString = Data; ptrString++; }while(*ptrString != 0); *ptrString = 0; // make sure string terminates w/ 0 ptrString = &ptrStartString; EnableSRAM(FALSE); }
void lcd_envia_byte( boolean endereco, byte dado ) { // coloca a linha rs em 0 output_low(lcd_rs); // aguarda o display ficar desocupado //while ( bit_test(lcd_le_byte(),7) ) ; // configura a linha rs dependendo do modo selecionado output_bit(lcd_rs,endereco); delay_us(100); // aguarda 100 us // caso a linha rw esteja definida, coloca em 0 #ifdef lcd_rw output_low(lcd_rw); #endif // desativa linha enable output_low(lcd_enable); // envia a primeira parte do byte lcd_envia_nibble(dado >> 4); // envia a segunda parte do byte lcd_envia_nibble(dado & 0x0f); }
void write_ext_eeprom(EEPROM_ADDRESS address, byte data) { byte cmd[3]; byte i; cmd[0]=data; cmd[1]=address; cmd[2]=0xa; for(i=1;i<=4;++i) shift_left(cmd,3,0); output_high(EEPROM_SELECT); for(i=1;i<=20;++i) { output_bit(EEPROM_DI, shift_left(cmd,3,0)); output_high(EEPROM_CLK); output_low(EEPROM_CLK); } output_low(EEPROM_DI); output_low(EEPROM_SELECT); delay_ms(11); }
byte read_ext_eeprom(EEPROM_ADDRESS address) { byte cmd[3]; byte i,data; cmd[0]=0; cmd[1]=address; cmd[2]=0xc; for(i=1;i<=4;++i) shift_left(cmd,3,0); output_high(EEPROM_SELECT); for(i=1;i<=20;++i) { output_bit(EEPROM_DI, shift_left(cmd,3,0)); output_high(EEPROM_CLK); output_low(EEPROM_CLK); if(i>12) shift_left(&data,1,input(EEPROM_DO)); } output_low(EEPROM_SELECT); return(data); }
void init_ext_eeprom() { byte cmd[2]; byte i; output_low(EEPROM_DI); output_low(EEPROM_CLK); output_low(EEPROM_SELECT); cmd[0]=0x80; cmd[1]=0x9; for(i=1;i<=4;++i) shift_left(cmd,2,0); output_high(EEPROM_SELECT); for(i=1;i<=12;++i) { output_bit(EEPROM_DI, shift_left(cmd,2,0)); output_high(EEPROM_CLK); output_low(EEPROM_CLK); } output_low(EEPROM_DI); output_low(EEPROM_SELECT); }
void vfd_putc(char data) { int8 i; output_low(VFD_SCK); if (input(VFD_MB)) { while (input(VFD_MB)) { } delay_us(10); } output_low(VFD_SS); for (i=0;i<8;i++) { output_bit(VFD_SIN,data & 0b10000000); output_high(VFD_SCK); output_low(VFD_SCK); #IFNDEF VFD_ULTRAFAST delay_cycles(4); #ENDIF data <<= 1; } output_high(VFD_SS); #IFNDEF VFD_ULTRAFAST delay_cycles(4); #ENDIF }
//----------------------------------------------------------------------- // load_data(int data) //----------------------------------------------------------------------- // void load_data(int data_in) { int i; int data; data = data_in; // Load data for(i=0; i < DATA_BITS; i++){ // Leading edge of Program clock output_low(SCLK); // put here code to shift data out on SDIO output_bit(SDIO, shift_left(&data, 1, 0 ) ); // Trailling edge of clock (data is clocked in ADNS-2051) output_high(SCLK); // Delay delay_us(25); } }