fork download
  1. /*
  2.  * File: TempSensor.c
  3.  * Author: narayan
  4.  *
  5.  * Created on 15 de Março de 2019, 22:29
  6.  */
  7.  
  8. #ifndef F_CPU
  9. # define F_CPU 20000000UL / 8
  10. #endif
  11.  
  12. #include <stdlib.h>
  13. #include <inttypes.h>
  14. #include <avr/io.h>
  15. #include <util/delay.h>
  16. #include <string.h>
  17.  
  18. #include "usart.h"
  19.  
  20. /*
  21.  *
  22.  */
  23.  
  24. #define WRITE_OP (0x00 << 0x00)
  25. #define READ_OP (0x01 << 0x00)
  26.  
  27. //Statuses handling
  28. #define STATUS_NACK 1
  29. #define STATUS_ACK 0
  30. #define STATUS_OK 0
  31.  
  32. #define ARBLOST -1
  33. #define BUSRROR -2
  34.  
  35. void CPU_Init(void){
  36. // Enable Protected registers write
  37. CPU_CCP = CCP_IOREG_gc;
  38. CLKCTRL_MCLKCTRLA = CLKCTRL_CLKSEL_OSC20M_gc;
  39.  
  40. // Eable Prescaler
  41. CPU_CCP = CCP_IOREG_gc;
  42. CLKCTRL_MCLKCTRLB = CLKCTRL_PDIV_8X_gc | CLKCTRL_PEN_bm;
  43. CPU_SREG |= CPU_I_bm; // Global Interrupt Flag Enabled
  44. }
  45.  
  46. void TWI_Init(void){
  47. TWI0_CTRLA |= TWI_SDASETUP_4CYC_gc // SDASETUP time of 4 clock cycles
  48. | TWI_SDAHOLD_500NS_gc; // SDA Hold time - Meets SMBus specifications across all corners (0x03) ???
  49. TWI0_CTRLA &= ~TWI_FMPEN_bm; // Fast Mode Plus disabled
  50.  
  51. // Set MBAUD to get 100KHz for SCL from fSCL = CLK_PER / (10 + 2*BAUD + CLK_PER*tRise)
  52. // fSCL = 104KHz, CLK_PER = 20MHz/8, tRise = 2.5ns (datasheet page 17)
  53. TWI0_MBAUD = 0x07;
  54. TWI0_MCTRLB |= TWI_FLUSH_bm; // Clear TWI state
  55. TWI0_MSTATUS |= TWI_RIF_bm // Set RIF
  56. | TWI_WIF_bm // Set WIF
  57. | TWI_BUSSTATE_IDLE_gc; // Manually set BUS state to IDLE
  58. TWI0_MCTRLA &= ~TWI_TIMEOUT_gm; // Disable Timeout for TWI operation
  59. TWI0_MCTRLA |= TWI_SMEN_bm // Smart Mode Enable
  60. | TWI_ENABLE_bm; // Enable Master Mode of operation
  61. }
  62.  
  63. void Start_Bit(void){
  64. // Wait until BUS is IDLE
  65. while( (TWI0_MSTATUS & TWI_BUSSTATE_gm) != TWI_BUSSTATE_IDLE_gc)
  66. /*wait*/;
  67.  
  68. // Send Start Bit
  69. TWI0_MCTRLB = TWI_MCMD_REPSTART_gc;
  70. }
  71.  
  72. void Stop_Bit(void){
  73. // Wait until BUS is IDLE
  74. while( (TWI0_MSTATUS & TWI_BUSSTATE_gm) != TWI_BUSSTATE_IDLE_gc)
  75. /*wait*/;
  76.  
  77. TWI0_MCTRLB = TWI_MCMD_STOP_gc;
  78. }
  79.  
  80. uint8_t TWI_Read(uint8_t sl_addr, uint8_t* packet, uint8_t* psumtemp, float* pavgtemp){
  81. if( (TWI0_MSTATUS & TWI_BUSERR_bm) || (TWI0_MSTATUS & TWI_ARBLOST_bm) ){
  82. TWI0_MCTRLA &= ~TWI_ENABLE_bm;
  83. _delay_ms(1);
  84. TWI0_MCTRLA |= TWI_ENABLE_bm;
  85. TWI0_MCTRLA |= TWI_BUSSTATE_IDLE_gc;
  86. }
  87.  
  88. // Send Slave address and R operation onto the bus
  89. TWI0_MADDR = (sl_addr << 1) | READ_OP;
  90.  
  91. for(uint8_t i = 0; i < 5; i++){
  92. // Data byte is stored into TWI0_DATA register and passed to *packet variable
  93. *(packet + i) = TWI0_MDATA;
  94. // Sum the read value
  95. *psumtemp += *(packet + i);
  96. // Average readed values
  97. *pavgtemp = *psumtemp / 5.0;
  98. if(i == 5){
  99. // Stop reading more data bytes with a NACK
  100. TWI0_MCTRLB = TWI_ACKACT_NACK_gc;
  101. break;
  102. }
  103. // Send ACK from Master to signal Data packed readed
  104. TWI0_MCTRLB = TWI_ACKACT_ACK_gc;
  105. }
  106.  
  107. // Send Stop bit to terminate transaction
  108. TWI0_MCTRLB = TWI_MCMD_STOP_gc;
  109.  
  110. return STATUS_OK;
  111. }
  112.  
  113. int main(int argc, char** argv) {
  114.  
  115. uint8_t temperature[256] = {0};
  116. uint8_t sumtemp = 0;
  117. float avgtemp = 0.0;
  118. char message[256];
  119.  
  120. CPU_Init();
  121. USART2_Init();
  122. TWI_Init();
  123.  
  124. while(0x01){
  125. TWI_Read(0x18, temperature, &sumtemp, &avgtemp);
  126. USART2_SendStr(itoa(avgtemp, message, 10));
  127. USART2_SendChar('\n');
  128. _delay_ms(1000);
  129. }
  130.  
  131. return (EXIT_SUCCESS);
  132. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:14:20: fatal error: avr/io.h: No such file or directory
 #include <avr/io.h>
                    ^
compilation terminated.
stdout
Standard output is empty