AnsweredAssumed Answered

I2C not working on STM8S103K3P6. what am I doing wrong?

Question asked by Rita Jonkute on Nov 15, 2017
Latest reply on Nov 24, 2017 by pavel a

I am trying to make I2C work with stm8s103 using  <iostm8s103f3.h> and <intrinsics.h> libraries. The controller has to read data from temperature sensor, which I could later use to show on OLED. Could anyone help, or maybe share a code to help?

 

#include <iostm8s103f3.h>
#include <intrinsics.h>
#include <stdint.h>

unsigned int time=0;
unsigned int adc_buffer[2] = {0,0};
unsigned int adc_value;
unsigned int Pulse;
unsigned int Signal;
unsigned int sampleCounter=0;
unsigned int nCount = 0;
unsigned int adc_value = 0;
unsigned int nAdc_Buffer[10] = {0,0,0,0,0,0,0,0,0,0};
unsigned int nAdc_Hold=0;
unsigned int BPM;

unsigned int pikas1=0;
unsigned int pikas2=0;
unsigned int laikas=0;

#define PIN_BIT_BANG_DATA PD_ODR_ODR4 //PD buvo
#define PIN_BIT_BANG_CLOCK PD_ODR_ODR5
#define PIN_ERROR PD_ODR_ODR6

//
// I2C device related constants.
//
#define DEVICE_ADDRESS 0x5A
#define I2C_READ 1
#define I2C_WRITE 0

//
// Buffer to hold the I2C data.
//
uint8_t i2c_address;
uint8_t value[5];
uint8_t txCount, i;
unsigned int _buffer[2];
unsigned int I2C_Tx_buffer[5];
int _nextByte = 0;

//
// Bit bang data on the diagnostic pins.
//
//void BitBang(unsigned char byte)
//{
// for (short bit = 7; bit >= 0; bit--)
// {
// if (byte & (1 << bit))
// {
// PIN_BIT_BANG_DATA = 1;
// }
// else
// {
// PIN_BIT_BANG_DATA = 0;
// }
// PIN_BIT_BANG_CLOCK = 1;
// __no_operation();
// PIN_BIT_BANG_CLOCK = 0;
// }
// PIN_BIT_BANG_DATA = 0;
//}

void InitialiseSystemClock()
{
CLK_ICKR = 0; // Reset the Internal Clock Register.
CLK_ICKR_HSIEN = 1; // Enable the HSI.
CLK_ECKR = 0; // Disable the external clock.
while (CLK_ICKR_HSIRDY == 0); // Wait for the HSI to be ready for use.
CLK_CKDIVR = 0; // Ensure the clocks are running at full speed.
CLK_PCKENR1 = 0xff; // Enable all peripheral clocks.
CLK_PCKENR2 = 0xff; // Ditto.
CLK_CCOR = 0; // Turn off CCO.
CLK_HSITRIMR = 0; // Turn off any HSIU trimming.
CLK_SWIMCCR = 0; // Set SWIM to run at clock / 2.
CLK_SWR = 0xe1; // Use HSI as the clock source.
CLK_SWCR = 0; // Reset the clock switch control register.
CLK_SWCR_SWEN = 1; // Enable switching.
while (CLK_SWCR_SWBSY != 0); // Pause while the clock switch is busy.
}

//
// Initialise the I2C system.
//
void InitialiseI2C()
{
I2C_CR1_PE = 0; // Disable I2C before configuration starts.

// Setup the clock information.
//
I2C_FREQR = 16; // Set the internal clock frequency (MHz).
I2C_CCRH_F_S = 0; // I2C running is standard mode.
I2C_CCRL = 0xa0; // SCL clock speed is 50 KHz.
I2C_CCRH_CCR = 0x00;
//
// Set the address of this device.
//
I2C_OARH_ADDMODE = 0; // 7 bit address mode.
//
I2C_OARL_ADD = 0;
//I2C_OARL_ADD = 0x5A;
//
I2C_OARH_ADDCONF = 1; // Docs say this must always be 1.
//
// Setup the bus characteristics.
//

I2C_TRISER = 17;
//
//
// Turn on the interrupts.
//
I2C_ITR_ITBUFEN = 1; // Buffer interrupt enabled.
I2C_ITR_ITEVTEN = 1; // Event interrupt enabled.
I2C_ITR_ITERREN = 1;
//
// Configuration complete so turn the peripheral on.
//
I2C_CR1_PE = 1;
//
// Enter master mode.
//
I2C_CR2_ACK = 1;
I2C_CR2_START = 1; //buvo
}


//
// I2C interrupts all share the same handler.
//
//void I2C_write_data (void) {
// i2c_address = DEVICE_ADDRESS << 1;
// value[0] = 0x06;
// value[1] = 0x02;
// txCount = 2;
// I2C_CR2_START = 1;
//
//}
#pragma vector = I2C_RXNE_vector
__interrupt void I2C_IRQHandler()
{
if (I2C_SR1_SB)
{
//
// Master mode, send the address of the peripheral we
// are talking to. Reading SR1 clears the start condition.
//
unsigned char reg = I2C_SR1;
//
// Send the slave address and the read bit.
//
I2C_DR = (DEVICE_ADDRESS) << 1; //i2c_address;//
//
// Clear the address registers.
//
I2C_OARL_ADD = 0;
I2C_OARH_ADD = 0;
return;
}
if (I2C_SR1_ADDR)
{
//
// In master mode, the address has been sent to the slave.
// Clear the status registers and wait for some data from the salve.
//
unsigned char reg = I2C_SR1;
reg = I2C_SR3;
return;
}
//
//// if (I2C_SR2_AF)
//// {
//// I2C_SR2_AF = 0; // End of slave transmission.
//// I2C_CR2_STOP = 1;
//// return;
//// }
if (I2C_SR1_TXE)
{
I2C_DR = 0x07;
I2C_DR = 0x07;
//}
//
// //I2C_CR2_STOP = 1;
// //}
return;
}

if (I2C_SR1_RXNE)
{
//
// The TMP102 temperature sensor returns two bytes of data
//
_buffer[_nextByte++] = I2C_DR;
if (_nextByte == 1)
{
I2C_CR2_ACK = 0;
I2C_CR2_STOP = 1;
}
// else
// {
// BitBang(_buffer[0]);
// BitBang(_buffer[1]);
// }
return;
}
//
// If we get here then we have an error so clear
// the error and continue.
//
// unsigned char reg = I2C_SR1;
// reg = I2C_SR3;
//
// Send a diagnostic signal to indicate we have cleared
// the error condition.
//
PIN_ERROR = 1;
__no_operation();
PIN_ERROR = 0;
}

#pragma vector=TIM4_OVR_UIF_vector //handler add ñòð 40 datashet
__interrupt void TIMR4_OVR(void){ //handler function ñòð 253
TIM4_SR_UIF=0; //ñáðàñûâàåì ôëàã ïðåðûâàíèÿ îò òàéìåðà
time++;
if (time > 500){
PB_ODR^=MASK_PB_ODR_ODR5;
time=0;}
}
void gpio_ (void){ // ñòð 106,110
PB_DDR_bit.DDR5 = 1; // pin PB5 as an output ñòð 111
PB_CR1_bit.C15 = 1; // output Push-pull ñòð 111
PB_CR2_bit.C25 = 1; // switching speed- äî 10 ÌÃö. ñòð 112
while(1){
PB_ODR_bit.ODR5=0;
for (unsigned int i = 30000;i>0;i--){asm("nop"); }//delay
PB_ODR_bit.ODR5=1;
for (unsigned int i = 30000;i>0;i--){asm("nop"); }//delay
}
}
void TIM2_PWM_ini(void){ // ñòð 26 datashet PC5
TIM2_PSCR=0x07; //Prescaler value/128 ñòð 241
TIM2_CR1_ARPE=1; //Auto-reload preload enable ñòð 226
TIM2_ARRH = (unsigned char)(999 >> 8); //Auto-reload register ñòð 241
TIM2_ARRL = (unsigned char)999; //Auto-reload register ñòð 242
TIM2_CCER1 = 0x30; //2 output enable 2 output polarity ñòð 238
TIM2_CCMR2 = 0x78; // Output compare 2 mode, Output compare 2 preload enable ñòð 236
TIM2_CCR2H = (unsigned char)(999 >> 8); //Capture/compare register ñòð 242
TIM2_CCR2L = (unsigned char)999; // Capture/compare register ñòð 243
//TIM2_CR1 |= 0x80; // MASK_TIM2_CR1_ARPE auto-reload preload enable ñòð 226
TIM2_CR1 |= 0x01; // MASK_TIM2_CR1_CEN Counter enabled ñòð 226
}
void TIM4_ini(void){
TIM4_CR1_CEN=1; //Counter enabled
TIM4_PSCR=0x07; //Prescaler value/128
TIM4_CR1_ARPE=1; //Auto-reload preload enable
TIM4_ARR=0xFF; //set value 255
TIM4_IER_UIE=1; //Update interrupt enable

}

void init_ADC1(void) //using ADC in single conversion mode
{
ADC_CR2_ALIGN=1; //Right alignment
ADC_CR1_CONT=0; //Single conversion mode
ADC_CR1_ADON=1; //Enable ADC
}
unsigned int read_ADC1(void) {
unsigned int val=0;
ADC_CR1_ADON=1; //Enable ADC and to start conversion
while(ADC_CSR_EOC == 0){;} //Wait till EOC
ADC_CSR_EOC = 0; //Reset FLAG
val |= (unsigned int)ADC_DRL; // Get result
val |= (unsigned int)ADC_DRH<<8; // Get result
val &= 0x03ff;
return (val);
}

int main( void )
{ init_ADC1();
__disable_interrupt();
InitialiseSystemClock();
InitialiseI2C();
//I2C_write_data();

//
// Initialise Port D.
//
PD_ODR = 0; // All pins are turned off. PD
PD_DDR_DDR4 = 1; // Port D, bit 4 is output.
PD_CR1_C14 = 1; // Pin is set to Push-Pull mode.
PD_CR2_C24 = 1; // Pin can run up to 10 MHz.
//
PD_DDR_DDR5 = 1; // Port D, bit 5 is output.
PD_CR1_C15 = 1; // Pin is set to Push-Pull mode.
PD_CR2_C25 = 1; // Pin can run up to 10 MHz.
//
PD_DDR_DDR6 = 1; // Port D, bit 6 is output.
PD_CR1_C16 = 1; // Pin is set to Push-Pull mode.
PD_CR2_C26 = 1; // Pin can run up to 10 MHz.

//InitialiseSystemClock();
//InitialiseI2C();
__enable_interrupt();

while(1){
//__wait_for_interrupt();

ADC_CSR_CH=0x03;
adc_buffer[0]=read_ADC1();
adc_value=adc_buffer[0];
if ((adc_value) < 17) {
adc_value=0; }
if ((adc_value > 17) && (adc_value < 93)) {
adc_value=1; }
if ((adc_value > 94) && (adc_value< 123)) {
adc_value=2; }
if ((adc_value> 124) && (adc_value< 152)) {
adc_value=3; }
if ((adc_value>153) && (adc_value< 184)) {
adc_value=4; }
if ((adc_value>185) && (adc_value< 218)) {
adc_value=5; }
if ((adc_value>219) && (adc_value< 249)) {
adc_value=6; }
if ((adc_value>250) && (adc_value< 282)) {
adc_value=7; }
if ((adc_value>283) && (adc_value< 310)) {
adc_value=8; }
if ((adc_value>311) && (adc_value < 343)) {
adc_value=9; }
if ((adc_value>344) && (adc_value< 377)) {
adc_value=10; }
if ((adc_value>378) && (adc_value< 446)) {
adc_value=11; }



ADC_CSR_CH=0x05;
adc_buffer[1]=read_ADC1();

for ( nCount = 0; nCount< 10; nCount++){
nAdc_Buffer[nCount]+=adc_buffer[1];

if (nAdc_Buffer[nCount]> 512) {
pikas1=nCount;
laikas=240/pikas1;}
for (int i=0; i<10; i++) {

nAdc_Hold+= laikas;}
BPM = nAdc_Hold / 10;

for ( int i = 0; i< 10; i++){
nAdc_Hold = 0;
}

time=0;
}

}}

Attachments

Outcomes