Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.25 KB | None | 0 0
  1. #include <io.h>
  2. #include <interrupt.h>
  3. #include <delay.h>
  4. #include <stdio.h>
  5. #include <mega32.h>
  6.  
  7. // Alphanumeric LCD functions
  8. #include <alcd.h>
  9.  
  10. // Declare your global variables here
  11.  
  12. // TWI functions
  13. #include <twi.h>
  14.  
  15. #ifndef cbi
  16. #define cbi(port,bit) (port)&=~(1<<(bit))
  17. #endif
  18. #ifndef sbi
  19. #define sbi(port,bit) (port)|=(1<<(bit))
  20. #endif
  21.  
  22. #define TWI_PORT PORTTC
  23. #define TWI_DDR DDRC
  24. #define TWI_PIN PINC
  25. #define SDA_PIN 6
  26. #define SCL_PIN 5
  27.  
  28. #define DS1307_SLA 0x68
  29. #define _222K10
  30. //#define 100K32
  31.  
  32. #define TWI_W 0
  33. #define TWI_R 1
  34.  
  35. #define TWI_START (1<<TWINT)|(1<<TWSTA)|(1<<TWEN) //0xA4 : START CONDITION
  36. #define TWI_STOP (1<<TWINT)|(1<<TWSTO)|(1<<TWEN) //0x94 : STOP CONDITION
  37. #define TWI_Clear_TWINT (1<<TWINT)|(1<<TWEN) //0x84 :XOA TWIN, NOT ACK
  38. #define TWI_Read_ACK (1<<TWINT)|(1<<TWEN)|(1<<TWEA) //0xC4 : XOA TWIN
  39.  
  40. void TWI_Init(void)
  41. {
  42. TWSR=0x00; //PRESCALER=1
  43. // TWBR=_100K;
  44. TWCR=(1<<TWINT)|(1<<TWEN);
  45. }
  46.  
  47.  
  48. unsigned char TWI_DS1307_wadr(unsigned char Addr)
  49. {
  50. TWCR=TWI_START; //goi START CONDITION
  51. while((TWCR&0x80)==0x00); //cho TWINT bit =1
  52. if((TWSR&0xF8)!=0x08) return TWSR; // NEU START CO LOI THI THOAT
  53.  
  54. TWDR=(DS1307_SLA<<1)+TWI_W; // dia chi DS va bit W
  55. TWCR=TWI_Clear_TWINT; // xoa TWINT, bat dau goi SLA
  56. while((TWCR&0x80)==0x00); // cho TWINT bit=1
  57. if((TWSR&0xF8)!=0x18) return TWSR; //co loi khi goi dia chi, thoat
  58.  
  59. TWDR=Addr; // GUI DIA CHI THANH GHI CAN GHI VAO
  60. TWCR=TWI_Clear_TWINT; //START SENT ADDRESS BY CLEANING TWINT
  61. while((TWCR&0x80)==0x00); // CHECK VA CHO TWINT BIT =1
  62. if((TWSR&0xF8)!=0x28) return TWSR; //NEU DU LIEU GUI KO THANH CONG, THOAT
  63. TWCR=TWI_STOP; // STOP CONDITION
  64. return 0;
  65. }
  66.  
  67. unsigned char TWI_DS1307_wblock(unsigned char Addr,unsigned char Data[],unsigned char len) // ghi 1 mang vao DS
  68. {
  69. unsigned char i;
  70. TWCR=TWI_START; // goi Start Condition
  71. while((TWCR&0x80)==0x00); //cho TWINT bit =1
  72. if((TWSR&0xF8)!=0x08) return TWSR; // Start co loi,thoat
  73.  
  74. TWDR=(DS1307_SLA<<1)+TWI_W; // dia chi DS va bit W
  75. TWCR=TWI_Clear_TWINT; // xoa TWINT de bat dau goi
  76. while((TWCR&0x80)==0x00); //cho TWINT bit=1
  77. if((TWSR&0xF8)!=0x18) return TWSR; //loi truyen SLA, thoat
  78.  
  79. TWDR=Addr; //goi thanh dia chi can ghi vao
  80. TWCR=TWI_Clear_TWINT; //xoa TWINT de bat dau goi
  81. while((TWCR&0x80)==0x00); //cho TWINT bit =1
  82. if((TWSR&0xF8)!=0x28) return TWSR; //loi gui dia chi thanh ghi,thoat
  83.  
  84. for (i=0;i<len;i=i++)
  85. {
  86. TWDR=Data[i]; //chuan bi xuat du lieu
  87. TWCR=TWI_Clear_TWINT; //xoa TWINT, bat dau send
  88. while((TWCR&0x80)==0x00); //cho TWINT bit=1
  89. if((TWSR&0xF8)!=0x28) return TWSR; // neu status ko bang 0x28 thi return
  90. }
  91. TWCR=TWI_STOP; // Stop condition
  92. return 0;
  93. }
  94.  
  95. unsigned char TWI_DS1307_rblock(unsigned char Data[],unsigned char len) // doc 1 mang tu DS
  96. {
  97. unsigned char i;
  98. TWCR=TWI_START; // GUI DIEU KIEN BAT DAU
  99. while(((TWCR&0x80)==0x00)||((TWSR&0xF8)!=0x08)); //TWINT=1 HOAC 0x08
  100.  
  101. TWDR=(DS1307_SLA<<1)+TWI_R; // GOI DIA CHI SLA + R
  102. TWCR=TWI_Clear_TWINT; //BAT DAU, XOA TWINT
  103. while(((TWCR&0x80)==0x00)||((TWSR&0xF8)!=0x40)); // TWINT =1 HOAC 0x40
  104.  
  105. //NHAN LEN-1 BYTE DAU TIEN
  106. for(i=0;i<len-1;i++)
  107. {
  108. TWCR=TWI_Read_ACK; //XOA TWINT, SE GOI ACK SAU KHI NHAN 1 BYTE MOI
  109. while(((TWCR&0x80)==0x00)||((TWSR&0xF8)!=0x50)); //TWINT =1 HOAC 0x50
  110. Data[i]=TWDR; //doc du lieu vao mang data
  111. }
  112. // nhan byte cuoi
  113. TWCR=TWI_Clear_TWINT; //XOA TWINT DE NHAN BYTE CUOI, SAU DO SET NOT ACK
  114. while(((TWCR&0x80)==0x00)||((TWSR&0xF8)!=0x58)); // cho TWINT=1 hoac 0x58
  115. Data[len-1]=TWDR;
  116. TWCR=TWI_STOP;
  117. return 0;
  118. }
  119. volatile unsigned char Second=55,Minute=59,Hour=11,Day=1,Date=31,Month=12,Year=17,Mode=1,AP=1;
  120. //chon che do 12h hay 24h, mode o bit 6 cua thanh ghi hours, Mode =1 :12h, Mode=0:24h
  121. //AP : nam o bit 5 cua thanh ghi Hours,AP=1:PM, AP=0:AM
  122.  
  123. volatile unsigned char tData[7],Time_count=0;
  124. char dis[5]; // bien dis dung de luu tru string hien thi len LCD
  125.  
  126. //doi BCD sang thap phan va nguoc lai
  127. unsigned char BCD2Dec(unsigned char BCD)
  128. {
  129. unsigned char L,H;
  130. L=BCD&0x0F;
  131. H=(BCD>>4)*10;
  132. return(H+L);
  133. }
  134. unsigned char Dec2BCD(unsigned char Dec)
  135. {
  136. unsigned char L,H;
  137. L=Dec%10;
  138. H=(Dec/10)<<4;
  139. return(H+L);
  140. }
  141. // chuong trinh con hien thi thoi gian doc dc tu DS1307
  142. void Display(void)
  143. {
  144. Second =BCD2Dec(tData[0]&0x7F);
  145. Minute =BCD2Dec(tData[1]);
  146. if(Mode!=0) Hour=BCD2Dec(tData[2]&0x1F); //mode 12h
  147. else Hour=BCD2Dec(tData[2]&0x3F); //mode 24h
  148.  
  149. Date =BCD2Dec(tData[4]);
  150. Month =BCD2Dec(tData[5]);
  151. Year =BCD2Dec(tData[6]);
  152.  
  153. lcd_clear();
  154. //in gio,phut,giay
  155. lcd_gotoxy(0,0);
  156. // lcd_puts("Time:");
  157. sprintf(dis,"Time:%d:%d:%d",Hour,Minute,Second);
  158. lcd_puts(dis);
  159. /* lcd_gotoxy(7,0); lcd_puts(dis); lcd_gotoxy(9,0); lcd_putchar('-');
  160. sprintf(dis,"%i",Minute);
  161. lcd_gotoxy(10,0); lcd_puts(dis); lcd_gotoxy(12,0); lcd_putchar('-');
  162. sprintf(dis,"%i",Second);
  163. lcd_gotoxy(13,0); lcd_puts(dis); */
  164.  
  165. if(Mode!=0) //Mode 12h
  166. {
  167. lcd_gotoxy(16,0);
  168. if(tData[2]=(AP<<5)) lcd_putchar('P');
  169. else lcd_putchar('A');
  170. }
  171. //in ngay,thang,nam
  172. lcd_gotoxy(0,1);
  173. //lcd_puts("Date:");
  174. sprintf(dis,"Date:2000+%d/%d/%d",Year,Month,Date);
  175. lcd_puts(dis);
  176. /* lcd_gotoxy(7,1);
  177. if(Year<10) lcd_putchar('0'); //neu nam <10, them so 0 ben trai
  178. lcd_puts(dis); lcd_gotoxy(9,1); lcd_putchar('-'); // in nam
  179. sprintf(dis,"%i",Month);
  180. lcd_gotoxy(10,1); lcd_puts(dis); lcd_gotoxy(12,1); lcd_putchar('-'); // in thang
  181. sprintf(dis,"Date:");
  182. lcd_gotoxy(13,1); lcd_puts(dis); //in ngay */
  183. }
  184.  
  185. void main(void)
  186. {
  187. // Declare your local variables here
  188.  
  189. // Input/Output Ports initialization
  190. // Port A initialization
  191. // Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In
  192. DDRA=(0<<DDA7) | (0<<DDA6) | (0<<DDA5) | (0<<DDA4) | (0<<DDA3) | (0<<DDA2) | (0<<DDA1) | (0<<DDA0);
  193. // State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T
  194. PORTA=(0<<PORTA7) | (0<<PORTA6) | (0<<PORTA5) | (0<<PORTA4) | (0<<PORTA3) | (0<<PORTA2) | (0<<PORTA1) | (0<<PORTA0);
  195.  
  196. // Port B initialization
  197. // Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In
  198. DDRB=(0<<DDB7) | (0<<DDB6) | (0<<DDB5) | (0<<DDB4) | (0<<DDB3) | (0<<DDB2) | (0<<DDB1) | (0<<DDB0);
  199. // State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T
  200. PORTB=(0<<PORTB7) | (0<<PORTB6) | (0<<PORTB5) | (0<<PORTB4) | (0<<PORTB3) | (0<<PORTB2) | (0<<PORTB1) | (0<<PORTB0);
  201.  
  202. // Port C initialization
  203. // Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In
  204. DDRC=(0<<DDC7) | (0<<DDC6) | (0<<DDC5) | (0<<DDC4) | (0<<DDC3) | (0<<DDC2) | (0<<DDC1) | (0<<DDC0);
  205. // State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T
  206. PORTC=(0<<PORTC7) | (0<<PORTC6) | (0<<PORTC5) | (0<<PORTC4) | (0<<PORTC3) | (0<<PORTC2) | (0<<PORTC1) | (0<<PORTC0);
  207.  
  208. // Port D initialization
  209. // Function: Bit7=Out Bit6=Out Bit5=Out Bit4=Out Bit3=Out Bit2=Out Bit1=Out Bit0=Out
  210. DDRD=(1<<DDD7) | (1<<DDD6) | (1<<DDD5) | (1<<DDD4) | (1<<DDD3) | (1<<DDD2) | (1<<DDD1) | (1<<DDD0);
  211. // State: Bit7=0 Bit6=0 Bit5=0 Bit4=0 Bit3=0 Bit2=0 Bit1=0 Bit0=0
  212. PORTD=(0<<PORTD7) | (0<<PORTD6) | (0<<PORTD5) | (0<<PORTD4) | (0<<PORTD3) | (0<<PORTD2) | (0<<PORTD1) | (0<<PORTD0);
  213.  
  214. // Timer/Counter 0 initialization
  215. // Clock source: System Clock
  216. // Clock value: Timer 0 Stopped
  217. // Mode: Normal top=0xFF
  218. // OC0 output: Disconnected
  219. TCCR0=(0<<WGM00) | (0<<COM01) | (0<<COM00) | (0<<WGM01) | (1<<CS02) | (0<<CS01) | (1<<CS00);
  220. TIMSK=(1<<TOIE0);
  221. TCNT0=0x00;
  222. OCR0=0x00;
  223.  
  224.  
  225.  
  226. // Timer(s)/Counter(s) Interrupt(s) initialization
  227. TIMSK=(0<<OCIE2) | (0<<TOIE2) | (0<<TICIE1) | (0<<OCIE1A) | (0<<OCIE1B) | (0<<TOIE1) | (0<<OCIE0) | (0<<TOIE0);
  228.  
  229. // External Interrupt(s) initialization
  230. // INT0: Off
  231. // INT1: Off
  232. // INT2: Off
  233. MCUCR=(0<<ISC11) | (0<<ISC10) | (0<<ISC01) | (0<<ISC00);
  234. MCUCSR=(0<<ISC2);
  235.  
  236.  
  237.  
  238. // TWI initialization
  239. // Mode: TWI Master
  240. // Bit Rate: 100 kHz
  241. twi_master_init(100);
  242.  
  243. // Alphanumeric LCD initialization
  244. // Connections are specified in the
  245. // Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
  246. // RS - PORTA Bit 0
  247. // RD - PORTA Bit 1
  248. // EN - PORTA Bit 2
  249. // D4 - PORTA Bit 4
  250. // D5 - PORTA Bit 5
  251. // D6 - PORTA Bit 6
  252. // D7 - PORTA Bit 7
  253. // Characters/line: 8
  254. lcd_init(16);
  255.  
  256. // Global enable interrupts
  257. #asm("sei")
  258. lcd_clear();
  259. lcd_gotoxy(0,0);
  260. lcd_puts("hello Ds1307");
  261. lcd_gotoxy(0,1);
  262. lcd_puts("anh khoa");
  263. delay_ms(100);
  264.  
  265. tData[0]=Dec2BCD(Second);
  266. tData[1]=Dec2BCD(Minute);
  267. if(Mode!=0)tData[2]=Dec2BCD(Hour)|(Mode<<6)|(AP<<5); //mode12h
  268. else tData[2]=Dec2BCD(Hour);
  269. tData[3]=Dec2BCD(Day);
  270. tData[4]=Dec2BCD(Date);
  271. tData[5]=Dec2BCD(Month);
  272. tData[6]=Dec2BCD(Year);
  273. TWI_Init(); //khoi dong TWI
  274. TWI_DS1307_wblock(0x00,tData,7); // ghi lien tiep cac bien thoi gian vao DS1307
  275.  
  276. delay_ms(1); // choDs1307 xu ly
  277.  
  278. //doc va hien thi thoi gian lan dau tien
  279. TWI_DS1307_wadr(0x00); //set dia chi ve 0
  280. delay_ms(1); // cho DS1307 xy ly
  281. TWI_DS1307_rblock(tData,7); //doc ca khoi thoi gian 7 byte
  282. Display(); //hien thi ket qua len LCD
  283. //-----------
  284.  
  285. while (1)
  286. {
  287. // Place your code here
  288.  
  289. }
  290.  
  291. }
  292. ISR(TIM0_OVF)
  293. {
  294. Time_count++;
  295. if(Time_count>=10)
  296. {
  297. //doc DS1307
  298. TWI_DS1307_wadr(0x00); // set dia chi ve 0
  299. delay_ms(1);
  300. TWI_DS1307_rblock(tData,7); //doc ca khoi thoi gian 7 byte
  301.  
  302. // hien thi ket qua len LCD
  303. if(BCD2Dec(tData[0])!=Second)
  304. {
  305. Second=BCD2Dec(tData[0]&0x7F);
  306. sprintf(dis,"%i",Second);
  307. lcd_gotoxy(13,0); lcd_puts(" ");
  308. lcd_gotoxy(13,0); lcd_puts(dis);
  309. if(Second==0) Display(); //moi phut cap nhat 1 lan
  310. }
  311. Time_count=0;
  312. }
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement