Jump to content

timo27

EstablishedMember
  • Content Count

    7
  • Joined

  • Last visited

    Never

Community Reputation

0 Neutral

About timo27

  • Rank
    Newbrie

Profile Information

  • Location
    Belgium
  1. hey, how do i make the internal ADC on the 16f628 work? i don't seem to figure that out in C2C. I know that it has te capabilities but it don't know how to do it. I checked the example code but that misses the header file and it doesn't compile in C2C or Sourceboost IDE. Anyone got a simple example? thx in advance! Timo
  2. That's seems to be very usefull! I'm gonna try that asap. thx for that code rminnig! Timo
  3. RicherM: I have no problem at all if you use my code. I'm just doing some testing for a project i'm into so it's nothing "secret" :-) Feel free to refer my name on it as well, i also have updated the I2C source code a bit so maybe i should post it to the examples as well. To all: I'm still looking for some code to read values from ports when the port inputs (RA0 & RA1) change, like inputs from buttons that get pressed. Any way i can get something usefull? I used times with interrupts allready but they missed sometimes the hit from the button when it got pressed. This code should be added on the code i posted first. Anyone got a good suggestion? Thx in advance, Timo
  4. Hi As an example i would like to generate an interrupt and read the inputs (pressed by buttons). Any possible code for that wich is usefull? I'm using the C2C C-compiler for PIC's (www.picant.com/c2c/). I have a PIC16F628 and pins RA0 and RA1 on port RA are still free for that. There i would connect 2 buttons on but their value should be read when they are pressed or something. I just need to see the edges when they are pressed. Greetings, Timo
  5. I found the problem , the comparator inputs caused the mess. I just had to turn off those comparator inputs and all works fine like on a PIC 16F84. Greetz Timo
  6. I think the problem is with the SDA pin since it has a double function, it can work as input and output. Now the thing is that it worked, and still works on a PIC16F84 :-) I switched to the PIC16F628 for a reason that the PIC 16F84 was used for 85% with that code. the 628 has the double but doesn't fucntion well with the SDA. I'll try to use a Pull-Up resistor now. If there is another replacement with that has teh same capabilities and / or outputs as the PIC16F84 plz let me know. It would be very helpfull! Thx in advance!! Timo
  7. Hey, I used the i2c protocols (code below) for the PIC16F84(A) and they work. When i change the PIC to a 16F628 it doesn't work anymore. I must have overlooked something but i don't seem to figure out what i missed... Does that have something to do with the open drain outputs? I tried to use ports on the RB or RA and they all mess up the i2c. Any suggestions ? This is the C2C source code i use: //Define device config block asm { list p=PIC16F628 __config H'3F29';XT,!WDT,!PWRT,!CP } #pragma CLOCK_FREQ 4000000 /* Definitions for the LCD interface */ #ifndef PORTB #define PORTB 0x06 #endif #ifndef TRISB #define TRISB 0x06 #endif #ifndef PORTA #define PORTA 0x05 #endif #ifndef TRISA #define TRISA 0x05 #endif #ifndef PinLCD_SEL #define PinLCD_SEL 2 #endif #ifndef PinLCD_RS #define PinLCD_RS 1 #endif #ifndef PinLCD_DATA_4 #define PinLCD_DATA_4 3 #endif #ifndef PinLCD_DATA_5 #define PinLCD_DATA_5 4 #endif #ifndef PinLCD_DATA_6 #define PinLCD_DATA_6 5 #endif #ifndef PinLCD_DATA_7 #define PinLCD_DATA_7 6 #endif #ifndef PortLCD_SEL #define PortLCD_SEL PORTB #endif #ifndef PortLCD_RS #define PortLCD_RS PORTB #endif #ifndef PortLCD_DATA_4 #define PortLCD_DATA_4 PORTB #endif #ifndef PortLCD_DATA_5 #define PortLCD_DATA_5 PORTB #endif #ifndef PortLCD_DATA_6 #define PortLCD_DATA_6 PORTB #endif #ifndef PortLCD_DATA_7 #define PortLCD_DATA_7 PORTB #endif #ifndef TrisLCD_SEL #define TrisLCD_SEL TRISB #endif #ifndef TrisLCD_RS #define TrisLCD_RS TRISB #endif #ifndef TrisLCD_DATA_4 #define TrisLCD_DATA_4 TRISB #endif #ifndef TrisLCD_DATA_5 #define TrisLCD_DATA_5 TRISB #endif #ifndef TrisLCD_DATA_6 #define TrisLCD_DATA_6 TRISB #endif #ifndef TrisLCD_DATA_7 #define TrisLCD_DATA_7 TRISB #endif /* Definitions for the I²C Interface */ #ifndef PinSDA #define PinSDA 4 #endif //OPen drain output #ifndef PinSCL #define PinSCL 3 #endif #ifndef PortSDA #define PortSDA PORTA #endif #ifndef PortSCL #define PortSCL PORTA #endif #ifndef TrisSDA #define TrisSDA TRISA #endif #ifndef TrisSCL #define TrisSCL TRISA #endif #define clear_lcd 0x01 /* Clear Display */ #define return_home 0x02 /* Cursor to Home position */ #define entry_mode 0x06 /* Normal entry mode */ #define entry_mode_shift 0x07 /* - with shift */ #define system_set_8_bit 0x38 /* 8 bit data mode 2 line ( 5x7 font ) */ #define system_set_4_bit 0x28 /* 4 bit data mode 2 line ( 5x7 font ) */ #define display_on 0x0c /* Switch ON Display */ #define display_off 0x08 /* Cursor plus blink */ #define set_dd_line1 0x80 /* Line 1 position 1 */ #define set_dd_line2 0xC0 /* Line 2 position 1 */ #define set_dd_line3 0x94 /* Line 3 position 1 */ #define set_dd_line4 0xD4 /* Line 4 position 1 */ #define set_dd_ram 0x80 /* Line 1 position 1 */ #define write_data 0x00 /* With rs = 1 */ #define cursor_on 0x0E /* Switch Cursor ON */ #define cursor_off 0x0C /* Switch Cursor OFF */ char LCD_gp; void Delay(void) { delay_ms(1); clear_wdt(); } void Write_8_Bit( char dh ) { Delay(); if (dh & 0x80) set_bit(PortLCD_DATA_7,PinLCD_DATA_7); else clear_bit(PortLCD_DATA_7,PinLCD_DATA_7); if (dh & 0x40) set_bit(PortLCD_DATA_6,PinLCD_DATA_6); else clear_bit(PortLCD_DATA_6,PinLCD_DATA_6); if (dh & 0x20) set_bit(PortLCD_DATA_5,PinLCD_DATA_5); else clear_bit(PortLCD_DATA_5,PinLCD_DATA_5); if (dh & 0x10) set_bit(PortLCD_DATA_4,PinLCD_DATA_4); else clear_bit(PortLCD_DATA_4,PinLCD_DATA_4); Delay(); set_bit(PortLCD_SEL, PinLCD_SEL); Delay(); clear_bit(PortLCD_SEL, PinLCD_SEL); clear_wdt(); } void Write_4_Bit(char dl ) { // clear_bit( LCD_CONTROL, LCD_WR ); /* Write mode */ if (dl & 0x80) set_bit(PortLCD_DATA_7,PinLCD_DATA_7); else clear_bit(PortLCD_DATA_7,PinLCD_DATA_7); if (dl & 0x40) set_bit(PortLCD_DATA_6,PinLCD_DATA_6); else clear_bit(PortLCD_DATA_6,PinLCD_DATA_6); if (dl & 0x20) set_bit(PortLCD_DATA_5,PinLCD_DATA_5); else clear_bit(PortLCD_DATA_5,PinLCD_DATA_5); if (dl & 0x10) set_bit(PortLCD_DATA_4,PinLCD_DATA_4); else clear_bit(PortLCD_DATA_4,PinLCD_DATA_4); Delay(); set_bit(PortLCD_SEL, PinLCD_SEL); Delay(); clear_bit(PortLCD_SEL, PinLCD_SEL); Delay(); dl <<= 4; if (dl & 0x80) set_bit(PortLCD_DATA_7,PinLCD_DATA_7); else clear_bit(PortLCD_DATA_7,PinLCD_DATA_7); if (dl & 0x40) set_bit(PortLCD_DATA_6,PinLCD_DATA_6); else clear_bit(PortLCD_DATA_6,PinLCD_DATA_6); if (dl & 0x20) set_bit(PortLCD_DATA_5,PinLCD_DATA_5); else clear_bit(PortLCD_DATA_5,PinLCD_DATA_5); if (dl & 0x10) set_bit(PortLCD_DATA_4,PinLCD_DATA_4); else clear_bit(PortLCD_DATA_4,PinLCD_DATA_4); Delay(); set_bit(PortLCD_SEL, PinLCD_SEL); Delay(); clear_bit(PortLCD_SEL, PinLCD_SEL); Delay(); } void DataMode(void) { set_bit(PortLCD_RS, PinLCD_RS ); Delay(); } void FunctionMode(void) { clear_bit(PortLCD_RS, PinLCD_RS ); Delay(); } void LcdClear(void) { FunctionMode(); Write_4_Bit(clear_lcd); DataMode(); } void Set8BitMode(void) { FunctionMode(); Write_4_Bit(system_set_8_bit); DataMode(); } void LcdSetup(void) { set_bit(STATUS, RP0); // select the register bank 1 clear_bit(TrisLCD_SEL,PinLCD_SEL); clear_bit(TrisLCD_RS,PinLCD_RS); clear_bit(TrisLCD_DATA_4,PinLCD_DATA_4); clear_bit(TrisLCD_DATA_5,PinLCD_DATA_5); clear_bit(TrisLCD_DATA_6,PinLCD_DATA_6); clear_bit(TrisLCD_DATA_7,PinLCD_DATA_7); clear_bit(STATUS, RP0); clear_bit(PortLCD_SEL,PinLCD_SEL); clear_bit(PortLCD_RS,PinLCD_RS); Delay(); Set8BitMode(); delay_ms(30); FunctionMode(); Write_8_Bit( system_set_4_bit ); /* This sequence resets the LCD */ delay_ms(5); Write_8_Bit( system_set_4_bit ); delay_ms(5); Write_8_Bit( system_set_4_bit ); delay_ms(5); Write_4_Bit( system_set_4_bit ); delay_ms(5); Write_4_Bit( display_off ); delay_ms(2); Write_4_Bit( entry_mode ); delay_ms(2); Write_4_Bit( display_on ); delay_ms(2); Write_4_Bit( set_dd_ram ); delay_ms(2); LcdClear(); DataMode(); } void LcdSetPos(char Pos) { FunctionMode(); Write_4_Bit( 0x80 + Pos ); DataMode(); } void LcdLine1(void) { FunctionMode(); Write_4_Bit( set_dd_line1 ); DataMode(); } void LcdLine2(void) { FunctionMode(); Write_4_Bit( set_dd_line2 ); DataMode(); } void LcdLine3(void) { FunctionMode(); Write_4_Bit( set_dd_line3 ); DataMode(); } void LcdLine4(void) { FunctionMode(); Write_4_Bit( set_dd_line4 ); DataMode(); } void LcdClearLine1(void) { LcdLine1(); for( LCD_gp = 0; LCD_gp < 20; LCD_gp++ ) { Write_4_Bit(' '); } LcdLine1(); } void LcdClearLine2(void) { LcdLine2(); for( LCD_gp = 0; LCD_gp < 20; LCD_gp++ ) { Write_4_Bit(' '); } LcdLine2(); } void LcdClearLine3(void) { LcdLine3(); for( LCD_gp = 0; LCD_gp < 20; LCD_gp++ ) { Write_4_Bit(' '); } LcdLine3(); } void LcdClearLine4(void) { LcdLine4(); for( LCD_gp = 0; LCD_gp < 20; LCD_gp++ ) { Write_4_Bit(' '); } LcdLine4(); } void LcdWrite( const char *lcdptr ) { char pi; char ch; pi = 0; // Check for end of string while( lcdptr[pi] != 0 ) { ch = lcdptr[pi++]; Write_4_Bit( ch );// Display on LCD } } void LcdWriteInt(int num) { Write_4_Bit( '0' + (num / 10000)); Write_4_Bit( '0' + (num / 1000) % 10 ); Write_4_Bit( '0' + (num / 100) % 10); Write_4_Bit( '0' + (num / 10) % 10 ); Write_4_Bit( '0' + num % 10 ); } void i2c_out (void) { set_bit(STATUS, RP0); // select the register bank 1 clear_bit(TrisSCL,PinSCL); //SCL = Output clear_bit(TrisSDA,PinSDA); //SDA = Output clear_bit(STATUS, RP0); delay_us(5); } void i2c_in (void) { set_bit(STATUS, RP0); // select the register bank 1 clear_bit(TrisSCL,PinSCL); //SCL = Output set_bit(TrisSDA,PinSDA); // SDA = Input clear_bit(STATUS, RP0); delay_us(5); } void i2c_go (void) { i2c_out(); set_bit(PortSCL,PinSCL); set_bit(PortSDA,PinSDA); delay_us(55); clear_bit(PortSDA,PinSDA); delay_us(15); clear_bit(PortSCL,PinSCL); delay_us(15); } void i2c_stop (void) { i2c_out(); set_bit(PortSCL,PinSCL); delay_us(25); set_bit(PortSDA,PinSDA); delay_us(15); } void write_i2c (char wr_byte) { char i=0; i2c_out(); while(i++<8) //1 OCTET { if (wr_byte & 0x80 ) set_bit(PortSDA,PinSDA); else clear_bit(PortSDA,PinSDA); wr_byte = wr_byte << 1; //clock set_bit(PortSCL,PinSCL); delay_us(15); clear_bit(PortSCL,PinSCL); delay_us(15); } } char read_i2c (void) { char i=0; char result=0; i2c_in(); for(i=0;i<8;i++) { delay_us(15); set_bit(PortSCL,PinSCL); delay_us(15); if ( (PortSDA>>PinSDA) & 1) result = result+1; //test if PortSDA.PinSDA==1 if (i<7) result = result << 1; delay_us(15); clear_bit(PortSCL,PinSCL); } return result; } void ack(void) { i2c_in(); set_bit(PortSCL,PinSCL); delay_us(25); clear_bit(PortSCL,PinSCL); delay_us(15); } void mnack (void) { i2c_out(); set_bit(PortSDA,PinSDA); delay_us(10); set_bit(PortSCL,PinSCL); delay_us(25); clear_bit(PortSCL,PinSCL); delay_us(15); clear_bit(PortSDA,PinSDA); delay_us(25); } void mack(void) { i2c_out(); clear_bit(PortSDA,PinSDA); delay_us(15); set_bit(PortSCL,PinSCL); delay_us(45); clear_bit(PortSCL,PinSCL); delay_us(15); } void main() { delay_us(5); unsigned char i=0; unsigned char data=0; unsigned char temp1=0; LcdSetup(); // setup LCD-Display delay_ms(100); // wait a little bit until Display is initialized LcdClearLine1(); LcdClearLine2(); LcdClearLine3(); LcdClearLine4(); begin: LcdClearLine1(); LcdClearLine2(); LcdClearLine3(); LcdClearLine4(); LcdLine1(); LcdWrite("I2C Test"); LcdLine2(); LcdWrite("8bit Schuifregister"); LcdLine3(); LcdWrite("PCF8574 adres 64"); LcdLine4(); LcdWrite("100ms per klokpuls"); data=255; for (i=0; i<10;i++) { i2c_go(); write_i2c(64); ack(); write_i2c(data); ack(); i2c_stop(); delay_us(15); data=data<<1; delay_ms(100); //wait per test } data=255; for (i=0; i<10;i++) { i2c_go(); write_i2c(64); ack(); write_i2c(data); ack(); i2c_stop(); delay_us(15); data=data>>1; delay_ms(100); //wait per test } LcdClearLine1(); LcdClearLine2(); LcdClearLine3(); LcdClearLine4(); goto begin; }
×
×
  • Create New...