1

Тема: cs4392

#define CPU_F   16000000 // Clock Speed
#define BAUD    9600 // USART Speed

#define CS  2
#define CLK 3
#define DIN 4
#define RST 5

void setup(){
  USART_Init(CPU_F/16/BAUD-1); 
  DDRD |= (1<<CS)|(1<<CLK)|(1<<DIN)|(1<<RST);
  PORTD &= ~(1<<DIN)|(1<<CS)|(1<<CLK)|(1<<RST);
  delay(500);
  PORTD |=(1<<RST);del();
  Write(0x05,0b00110000);
  
  Write(0x01,0b10100010);
  Write(0x02,0b01001001);
  Write(0x03,0b00000000);
  Write(0x04,0b00000000);
  Write(0x06,0b00000000);
  del();
  Write(0x05,0b00100000);
  }

void loop(){

  }

void Write(byte reg, byte din){  // WRITE_REG 
  PORTD &=~(1<<CS);del();
     byte addr = 0b00100000;
     for(char i = 7; i >= 0; i--){
        if(((addr >> i) & 0x01)==1){PORTD |=(1<<DIN);}else{PORTD &=~(1<<DIN);}
        PORTD |=(1<<CLK);del();PORTD &=~(1<<CLK);del();
        }
     for(char i = 7; i >= 0; i--){
        if(((reg >> i) & 0x01)==1){PORTD |=(1<<DIN);}else{PORTD &=~(1<<DIN);}
        PORTD |=(1<<CLK);del();PORTD &=~(1<<CLK);del();
        }
     for(char i = 7; i >= 0; i--){
        if(((din >> i) & 0x01)==1){PORTD |=(1<<DIN);}else{PORTD &=~(1<<DIN);}
        PORTD |=(1<<CLK);del();PORTD &=~(1<<CLK);del();
        }
        PORTD |=(1<<CS);
        PORTD &=~(1<<DIN);
        del();
  } 


void del(){delayMicroseconds(10);}  

void USART_Init( unsigned int ubrr ){
  UBRR0H = (unsigned char)(ubrr>>8);
  UBRR0L = (unsigned char)ubrr;
  UCSR0B = (1<<RXEN0)|(1<<TXEN0);
  UCSR0C = (1<<USBS0)|(3<<UCSZ00);
}  

void USART_String(char *data){
  for(int i=0;i<strlen(data);i++){
  while(!( UCSR0A & (1 << UDRE0 )));
  UDR0 = data[i];}
}

void USART_int(int to_int){
  String stringVar = String(to_int, DEC);
  char charVar[sizeof(stringVar)];
  stringVar.toCharArray(charVar, sizeof(charVar));
  USART_String(charVar);}