You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
KED/libraries/examples/i2c/main.c

190 lines
4.3 KiB

#include "main.h"
#include "delay.h"
#include "deviceSetup.h"
#include "usart.h"
#include "ascii.h"
#include "timer.h"
#include "i2c.h"
void timer_test(timerNo_t timer, pinNo_t pin)
{
timerInitCounter(timer, 4000, 999, downCounting);
timerSart(timer);
for(int i = 0; i < 10 ; i++)
{
while(!timerGetUpdateInterrupt(timer));
timerClearUpdateInterrupt(timer);
pinToggle(pin);
}
pinWrite(pin,0);
}
void timer_capture_compare_test(timerNo_t timer)
{
uint16_t i = 0;
timerInitCounter(timer, 100, 99, downCounting);
// We use pin PA3 (Arduino header A2)
//timerInitOutputCompare(timer, toggle, 4, pinA3, 2,0, 300);
timerInitOutputCompare(timer, pwm_normal, 2, pinB3, 2,0, 900);
timerSart(timer);
while(1){
delayMs(200);
timerSetCounterCompareValue(timer, 2, i);
i += 10;
if(i>99) i = 0;
}
}
void printBinary32(uint32_t toPrint, uint8_t lsbFirst)
{
int i;
char pt;
if(lsbFirst)
{
print_Usart(usart2, "Bit Pos | 0| 1| 2| 3| 4| 5| 6| 7| 8| 9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|");
print_Usart(usart2, "\n\r");
print_Usart(usart2, "Bits |");
for(i=0; i < 32; i++)
{
pt = (toPrint >> i) & 1;
usartSendChar(usart2, ' ');
usartSendChar(usart2, pt + 0x30);
usartSendChar(usart2, '|');
}
}
else
{
print_Usart(usart2, "Bit Pos |31|30|29|28|27|26|25|24|23|22|21|20|19|18|17|16|15|14|13|12|11|10| 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|");
print_Usart(usart2, "\n\r");
print_Usart(usart2, "Bits |");
for(i=0; i < 32; i++)
{
pt = (toPrint >> (31-i)) & 1;
usartSendChar(usart2, ' ');
usartSendChar(usart2, pt + 0x30);
usartSendChar(usart2, '|');
}
}
}
void printBinary8(uint8_t toPrint, uint8_t lsbFirst)
{
int i;
char pt;
if(lsbFirst)
{
print_Usart(usart2, "Bit Pos | 0| 1| 2| 3| 4| 5| 6| 7|");
print_Usart(usart2, "\n\r");
print_Usart(usart2, "Bits |");
for(i=0; i < 8; i++)
{
pt = (toPrint >> i) & 1;
usartSendChar(usart2, ' ');
usartSendChar(usart2, pt + 0x30);
usartSendChar(usart2, '|');
}
}
else
{
print_Usart(usart2, "Bit Pos | 7| 6| 5| 4| 3| 2| 1| 0|");
print_Usart(usart2, "\n\r");
print_Usart(usart2, "Bits |");
for(i=0; i < 8; i++)
{
pt = (toPrint >> (7-i)) & 1;
usartSendChar(usart2, ' ');
usartSendChar(usart2, pt + 0x30);
usartSendChar(usart2, '|');
}
}
}
int main(int argc, char *argv[])
{
uint8_t i = 0;
uint16_t slaveAddress = 0xC0;
uint8_t registerToRead = 0x00;
uint8_t i2cRecieved = 0;
uint8_t i2cData = 0xFF;
uint8_t i2cDataLenght = 1;
i2c_t i2c_1;
delayInitMs(8000000, 1000); // Clock Freq and Divider for ARM library
pinConfig(pinB3, output, pushPull, def_res, def_speed);
pinConfig(pinA0, input, def_stage, pullDown, def_speed);
setupInit(); // This is the sescond call of System init the assebly start code is calling it before the main.
usartInit( usart2,
pinA2,
pinA15,
115200,
eight,
NO_PARITY_CTRL,
noFlowControl);
//clears screen and send the wellcome messgae
print_Usart(usart2, ASCII_clear);
print_Usart(usart2, "Hello to our KED project\n\r");
//blinks 10 times to indicate the sicsessfull init if the device
for(i = 0 ; i < 2 ; i++) {
delayMs(100);
pinToggle(pinB3);
delayMs(100);
}
pinWrite(pinB3,0);
print_Usart(usart2, "\n\r");
i2c_init(&i2c_1, I2C_CH_1, i2c_mode_master, 0x00,0x00, i2c_address_count_single, i2c_address_size_7b, i2c_clk_speed_standart, i2c_clk_stretching_disable, i2c_wake_disabled);
slaveAddress = 0x40;
registerToRead = 0x02;
registerToRead = 0x06;
i2cData = 0xAA;
print_Usart(usart2, "Register Address To Write \n\r");
printBinary8(registerToRead,0);
print_Usart(usart2, "\n\r");
print_Usart(usart2, "\n\r");
print_Usart(usart2, "Data To Sent\n\r");
printBinary8(i2cData,0);
print_Usart(usart2, "\n\r");
print_Usart(usart2, "\n\r");
i2c_check_device(&i2c_1, &slaveAddress);
i2c_write(&i2c_1, &slaveAddress, &registerToRead, &i2cData, &i2cDataLenght);
i2c_read(&i2c_1, &slaveAddress, &registerToRead, &i2cRecieved,&i2cDataLenght);
print_Usart(usart2, "Data Recieved\n\r");
printBinary8(i2cRecieved,0);
print_Usart(usart2, "\n\r");
print_Usart(usart2, "\n\r");
print_Usart(usart2, "All is working fine\n\r");
while(1)
{
delayMs(100);
pinToggle(pinB3);
delayMs(100);
}
return 1;
}