/* This program was used to find a fault in the software. Note that the in the function
write_byte the clk wa not pulled low after send 1 bit hence the next bit was not sent.
Adiitionally note that before initialization the clk was again pulled low for
extra precaution*/
#include "hal.h"
#include "bitmap.h"
// define pins
#define CLK A_1
#define CS A_3
#define DIN A_5
//define here number of matrix modules used
#define number_of_modules 2
void setup(){
matrix_init();
write_same_display(fill_all);//fill_all is array in bitmap.h that fills all rows
}
void loop(){
}
//active low cs
void cs_active()
{
pin_write(CS,0);
}
void cs_idle()
{
pin_write(CS,1);
}
void clk_high(){
pin_write(CLK,1);
}
void clk_low(){
pin_write(CLK,0);
}
void write_byte(volatile uint8_t user_byte)
{
//Send 8 bits starting from MSB
for(volatile uint8_t bits=0;bits<8;bits++)
{
user_byte&0x80?pin_write(DIN,1):pin_write(DIN,0);
user_byte=user_byte<<1;
clk_high();
for(volatile uint8_t i=0;i<2;i++);//keep signal stable for some time
clk_low();//Deliberatily pull it low for next bit
}
}
void send(volatile uint8_t addr,volatile uint8_t data)
{
cs_active();
for(volatile uint8_t i=0;i<number_of_modules;i++)
{
write_byte(addr);
write_byte(data);
}
cs_idle();
}
void matrix_init(){
init_pin(CLK,output);
init_pin(CS,output);
init_pin(DIN,output);
clk_low();//make clk low while initialization
cs_idle();//make CS idle while initialization
send(0x0C,0x01);//Normal mode
send(0x09,0x00);//No decoding
send(0x0A,0x08);//Intensity 17/32
send(0x0B,0x07);
}
//A testing function to check if all modules are working properly
//This will display same output through all modules
void write_same_display(volatile uint8_t *data_Arr)
{
for(volatile uint8_t row=1;row<9;row++)
{
cs_active();
for(volatile uint8_t modules=0;modules<number_of_modules;modules++)
{
write_byte(row);
write_byte(data_Arr[row-1]);
}
cs_idle();
}
}