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 0x01
  29. #define STATUS_ACK 0x00
  30. #define STATUS_OK 0x00
  31.  
  32. void CPU_Init(void){
  33. // Enable Protected registers write
  34. CPU_CCP = CCP_IOREG_gc;
  35. CLKCTRL_MCLKCTRLA = CLKCTRL_CLKSEL_OSC20M_gc;
  36.  
  37. // Disable Prescaler
  38. CPU_CCP = CCP_IOREG_gc;
  39. CLKCTRL_MCLKCTRLB = CLKCTRL_PDIV_64X_gc | CLKCTRL_PEN_bm;
  40. }
  41.  
  42. void TWI_Init(void){
  43. TWI0_CTRLA |= TWI_SDASETUP_4CYC_gc // SDASETUP time of 4 clock cycles
  44. | TWI_SDAHOLD_500NS_gc; // SDA Hold time - Meets SMBus specifications across all corners (0x03) ???
  45. TWI0_CTRLA &= ~TWI_FMPEN_bm; // Fast Mode Plus disabled
  46.  
  47. // Set MBAUD to get 100KHz for SCL from fSCL = CLK_PER / (10 + 2*BAUD + CLK_PER*tRise)
  48. // fSCL = 104KHz, CLK_PER = 20Mhz/8, tRise = 2.5ns (datasheet page 17)
  49. TWI0_MBAUD = 0x07;
  50. TWI0_CTRLA &= ~TWI_TIMEOUT0_bm; // Disable Timeout for TWI operation
  51. TWI0_MSTATUS = TWI_BUSSTATE_IDLE_gc; // Manually set the Bus state to IDLE for TWI operation
  52. PORTMUX_TWISPIROUTEA = PORTMUX_TWI0_DEFAULT_gc;// Set default port and pins for SDA and SCL lines
  53. PORTA_DIR |= PIN3_bm | PIN2_bm;
  54. TWI0_MCTRLA = TWI_ENABLE_bm; // Enable Master Mode of operation
  55. }
  56.  
  57. void Start_Bit(void){
  58. // Wait until BUS is IDLE
  59. while( (TWI0_MSTATUS & TWI_BUSSTATE_gm) == TWI_BUSSTATE_BUSY_gc)
  60. /*wait*/;
  61.  
  62. // Send Start Bit
  63. TWI0_MCTRLB = TWI_MCMD_REPSTART_gc;
  64. }
  65.  
  66. void Stop_Bit(void){
  67. // Wait until BUS is IDLE
  68. while( (TWI0_MSTATUS & TWI_BUSSTATE_gm) == TWI_BUSSTATE_BUSY_gc)
  69. /*wait*/;
  70.  
  71. TWI0_MCTRLB = TWI_MCMD_STOP_gc;
  72. }
  73.  
  74. uint8_t TWI_Read(uint8_t sl_addr, uint8_t* packet, uint8_t* psumtemp, float* pavgtemp){
  75. // Make sure BUS is not busy
  76. while((TWI0_MSTATUS & TWI_BUSSTATE_BUSY_gc) == TWI_BUSSTATE_BUSY_gc)
  77. /*wait*/;
  78.  
  79. // Send Slave address and R operation onto the bus
  80. TWI0_MADDR = (sl_addr << 1) | READ_OP;
  81.  
  82. // Check Slave device ACK/NACK bit to signal address not/acknowledgment
  83. if(TWI0_MSTATUS & TWI_RXACK_bm){
  84. return STATUS_NACK; // Slave Address NOT received
  85. }
  86.  
  87. for(uint8_t i = 0; i < 256; i++){
  88. // Data byte is stored into TWI0_DATA register and passed to *packet variable
  89. *(packet + i) = TWI0_MDATA;
  90. // Send ACK from Master to signal Data packed readed
  91. TWI0_MCTRLB = TWI_ACKACT_ACK_gc;
  92. // Sum the readed value
  93. *psumtemp += *(packet + i);
  94. }
  95. // Send NACK from Master to signal don't need more data
  96. TWI0_MCTRLB = TWI_ACKACT_NACK_gc;
  97.  
  98. // Send Stop bit to terminate transaction
  99. TWI0_MCTRLB = TWI_MCMD_STOP_gc;
  100.  
  101. // Average readed values
  102. *pavgtemp = *psumtemp / 256.0;
  103.  
  104. // Check Master device ACK/NACK bit to signal data not/acknowledgment
  105. //if (TWI0_MSTATUS & TWI_RXACK_bm)
  106. //return STATUS_NACK;
  107.  
  108. return STATUS_OK;
  109. }
  110.  
  111. int main(int argc, char** argv) {
  112. uint8_t temperature[256] = {0};
  113. uint8_t sumtemp = 0;
  114. float avgtemp = 0.0;
  115. char message[256];
  116.  
  117. CPU_Init();
  118. USART2_Init();
  119. TWI_Init();
  120.  
  121. while(0x01){
  122. TWI_Read(0x18, temperature, &sumtemp, &avgtemp);
  123. USART2_SendStr(itoa(avgtemp, message, 10));
  124. USART2_SendChar('\n');
  125. _delay_ms(1000);
  126. }
  127.  
  128. return (EXIT_SUCCESS);
  129. }
  130.  
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