Hi people,
I am trying to introduce a delay (some appropriate value - needs trial and error) between clock pulses 9 and 10. I have attached the code here on which I am working currently. I am pretty new to arduino and unsure what approach to look into. My understanding of timers, interrupts, counters is premature. An example would definitely help.
//PROGRAM TO WRITE AN 8-BIT CONFIGURATION REGISTER TO EM4097 USING SERIAL INTERFACE
#define F_CPU 16000000;
#define EM_IN 10 // define here the pin which you connect to the EM4097 'IN' pin
#define EM_OUT 11 // define here the pin which you connect to the EM4097 'OUT' pin
#define EM_CLK 9 // define here the pin you connect to the EM4097 'CLK' pin
// Initialise serial pins for the EM4097 interface
//
//uint8_t incomingByte = 0;
int count = 0;
void setup(){
EM_setup();
/* noInterrupts();
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 31250; // compare match register 16MHz/256/2Hz
TCCR1B |= (1 << WGM12); // CTC mode
TCCR1B |= (1 << CS12); // 256 prescaler
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
interrupts(); // enable all interrupts */
}
void EM_setup()
{
Serial.begin(9600);
pinMode( EM_IN, OUTPUT );
digitalWrite( EM_IN, LOW );
//Serial.println(EM_IN);
pinMode( EM_OUT, INPUT_PULLUP );
pinMode( EM_CLK, OUTPUT );
digitalWrite( EM_CLK, LOW );
//Serial.println(EM_OUT);
}
// Do a clock reset on the EM4097
//
void EM_reset()
{
EM_setup();
delayMicroseconds(10); //5us is equal to 8us when simulated, please check it and rectify to sync with 125kHz(8us)internal PLL
digitalWrite( EM_CLK, HIGH );
delayMicroseconds( 10 );
digitalWrite( EM_IN, HIGH );
delayMicroseconds( 10 );
digitalWrite( EM_CLK, LOW );
delayMicroseconds( 10 );
digitalWrite( EM_IN, LOW );
}
// Write one bit to the EM4097
//
void EM_writeBit( uint8_t bit0 )//would it take the LSB bit everytime out of the 8bits from config
{
//for( int i = 0; i < 8; i++){
digitalWrite( EM_IN, bit0 & 1 );
//Serial.println("Input value is:");
//Serial.println(EM_IN);
delayMicroseconds( 1 );
digitalWrite( EM_CLK, HIGH );
delayMicroseconds( 1 );
digitalWrite( EM_CLK, LOW );
delayMicroseconds( 1 );
//count++;
//if(count = 8){
//delayMicroseconds(5);
//}
/*delayMicroseconds( 25 );
for(int i = 10; i < 13; i++){
digitalWrite( EM_IN, bit0 & 1 );
delayMicroseconds( 1 );
digitalWrite( EM_CLK, HIGH );
delayMicroseconds( 1 );
digitalWrite( EM_CLK, LOW );
delayMicroseconds( 1 );
}*/
}
/*void EM_ReadBit( uint8_t bit0)
{
delayMicroseconds( 10 );
digitalRead( EM_OUT );
//val = digitalRead( EM_OUT );
}*/
// Write config register
// passed: 8-bit config value (bit0 == config Bit#1, bit7= config Bit #8)
//
void EM_writeConfig( uint8_t config )
{
EM_writeBit( 0 ); // initial clk pulse
for( int i=0; i < 8; i++ )
{
EM_writeBit( config ); // write out each bit
config= config >> 1; // and shift down the bits
//count++;
//if( count = 8 ){
//delayMicroseconds( 5 );
/*digitalWrite( EM_CLK, HIGH);
delayMicroseconds( 1 );
digitalWrite( EM_CLK, LOW);
delayMicroseconds( 1 );*/
}
//Serial.println("data is:");
//Serial.println(config);
//Serial.println("output data is:");
//Serial.println(EM_OUT);
/* for( int j=10; j < 12; j++)
{
delayMicroseconds( 10 );
val = digitalRead( EM_OUT );
if(Serial.available() > 0){
incomingByte = Serial.read();
}
}*/
}
/*void EM_readStatus(uint8_t )
{ EM_ReadBit( 0 );
for(int j = 10; j < 12; j++){
EM_ReadBit( config );
config= config >> 1;
}
}*/
void loop()
{
// in here you can now:
uint8_t config= 0x96;//change the value of this variable based on the datasheet, value in binary is 10010110
EM_reset();
EM_writeConfig( config );
//Serial.println("output data is:");
//Serial.println(EM_OUT);
}
AA