{"id":944,"date":"2011-03-26T20:32:21","date_gmt":"2011-03-26T13:32:21","guid":{"rendered":"http:\/\/www.payitforward.edu.vn\/wordpress\/?page_id=944"},"modified":"2012-11-13T22:30:53","modified_gmt":"2012-11-13T15:30:53","slug":"bai-6-i2c","status":"publish","type":"page","link":"https:\/\/payitforward.edu.vn\/wordpress\/tutorials\/hitech-pic\/bai-6-i2c\/","title":{"rendered":"B\u00e0i 6 I2C"},"content":{"rendered":"<p>T\u00e0i li\u1ec7u n\u00e0y \u0111\u01b0\u1ee3c bi\u00ean so\u1ea1n kh\u00f4ng nh\u1eb1m v\u00e0o b\u1ea5t k\u1ef3 m\u1ee5c \u0111\u00edch n\u00e0o mang t\u00ednh th\u01b0\u01a1ng m\u1ea1i. B\u1ea1n c\u00f3 quy\u1ec1n sao ch\u00e9p, s\u1eeda ch\u1eefa v\u00e0 ph\u00e2n ph\u00e1t b\u1eb1ng  b\u1ea5t     k\u1ef3 h\u00ecnh th\u1ee9c n\u00e0o. H\u00e3y s\u1eed d\u1ee5ng t\u00e0i li\u1ec7u n\u00e0y, s\u1eeda ch\u1eefa, b\u1ed5 sung v\u00e0  g\u1eedi   cho   nh\u1eefng ai c\u1ea7n n\u00f3. Ch\u00fang t\u00f4i kh\u00f4ng \u0111\u1ec1 t\u00ean t\u00e1c gi\u1ea3 bi\u00ean so\u1ea1n.  T\u00e0i   li\u1ec7u  n\u00e0y  thu\u1ed9c quy\u1ec1n s\u1edf h\u1eefu nh\u1eefng ai \u0111ang d\u00f9ng n\u00f3.<\/p>\n<p>Pay it forward\u2026<\/p>\n<h2 style=\"text-align: center;\"><span style=\"text-decoration: underline;\">B\u00e0i 6:<\/span><\/h2>\n<h1 style=\"text-align: center;\">I2C<\/h1>\n<p><em><span style=\"color: #000080;\"><strong>M\u1ee5c ti\u00eau<\/strong><\/span><br \/>\nH\u1ecdc l\u1eadp tr\u00ecnh giao ti\u1ebfp I2C.<br \/>\n\u0110\u1ecdc gi\u00e1 tr\u1ecb \u0111\u1ed3ng h\u1ed3 s\u1ed1 (DS1307) (hi\u1ec3n th\u1ecb l\u00ean LCD), h\u1eb9n gi\u1edd b\u1eadt\/t\u1eaft t\u1ea3i AC (\u0111\u00e8n, qu\u1ea1t, m\u00e1y b\u01a1m, \u2026)<\/em><\/p>\n<p><span style=\"color: #000080;\"><strong>I. PH\u1ea6N C\u1ee8NG<\/strong><\/span><\/p>\n<p>S\u01a1 \u0111\u1ed3 nguy\u00ean l\u00fd<\/p>\n<p style=\"text-align: center;\">\n<div style=\"width: 567px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\" \" title=\"ds1307\" src=\"http:\/\/i653.photobucket.com\/albums\/uu259\/rockc4\/CLB%2013112010\/PIC%20course\/DS1307module.jpg\" alt=\"\" width=\"557\" height=\"314\" \/><p class=\"wp-caption-text\">DS1307 module<\/p><\/div>\n<div style=\"width: 567px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\" \" title=\"TRIAC module\" src=\"http:\/\/i653.photobucket.com\/albums\/uu259\/rockc4\/CLB%2013112010\/PIC%20course\/TRIACmodule.jpg\" alt=\"\" width=\"557\" height=\"296\" \/><p class=\"wp-caption-text\">TRIAC module<\/p><\/div>\n<p>H\u01b0\u1edbng d\u1eabn th\u1ef1c hi\u1ec7n m\u1ea1ch DS1307 + TRIAC, s\u01a1 \u0111\u1ed3 nguy\u00ean l\u00fd + Layout, c\u00e1c linh ki\u1ec7n trong b\u00e0i c\u00e1c b\u1ea1n xem \u1edf <a href=\"http:\/\/www.payitforward.edu.vn\/forum\/threads\/29\/\">b\u00e0i n\u00e0y<\/a><\/p>\n<p>&#8211;&gt; Tham kh\u1ea3o gi\u00e1 linh ki\u1ec7n \ud83d\ude42 t\u1ea1i lu\u1ed3ng <a href=\"http:\/\/www.payitforward.edu.vn\/forum\/threads\/28\/\">n\u00e0y<\/a><\/p>\n<p><strong><span style=\"color: #000080;\">II. K\u1ebeT N\u1ed0I:<\/span><\/strong><\/p>\n<p><strong>&#8211; Ngu\u1ed3n:<\/strong><br \/>\n2 module Ds1307 v\u00e0 Triac trong board ho\u00e0n to\u00e0n \u0111\u1ed9c l\u1eadp v\u1edbi nhau, kh\u00f4ng chung ngu\u1ed3n 5V v\u00e0 GND.<br \/>\nTuy nhi\u00ean tr\u00ean board c\u00f3 2 c\u00e1i jumper (m\u00ecnh h\u00e0n 2 c\u00e1i header 2) d\u00f9ng \u0111\u1ec3 n\u1ed1i VCC 2 module v\u00e0 GND 2 module trong tr\u01b0\u1eddng h\u1ee3p mu\u1ed1n c\u1ea5p 1 ngu\u1ed3n 5V cho c\u1ea3 2 module c\u00f9ng ho\u1ea1t \u0111\u1ed9ng.<\/p>\n<p>Trong b\u00e0i n\u00e0y, ch\u00fang ta s\u1ebd \u0111\u1ecdc th\u1eddi gian t\u1eeb DS1307 v\u1ec1 \u0111\u1ec3 k\u00edch TRIAC, n\u00ean ta s\u1ebd c\u1ea5p 1 ngu\u1ed3n 5V (ngu\u1ed3n t\u1eeb board MainPIC) cho c\u1ea3 2 module. V\u00ec v\u1eady:<\/p>\n<p><em>+ D\u00f9ng 2 c\u00e1i jumper \/ ho\u1eb7c 2 c\u00e1i bus 1 \u0111\u1ec3 n\u1ed1i 2 th\u1eb1ng VCC v\u00e0 2 th\u1eb1ng GND l\u1ea1i v\u1edbi nhau (xem h\u00ecnh: jumper m\u00e0u xanh v\u00e0 m\u00e0u v\u00e0ng)<br \/>\n+ C\u1ea5p ngu\u1ed3n 5V t\u1eeb m\u1ea1ch mainPIC sang: c\u00f3 2 ch\u1ed7 \u0111\u1ec3 c\u1eafm d\u00e2y, v\u00ed d\u1ee5 nh\u01b0 trong h\u00ecnh, s\u1ee3i v\u00e0ng l\u00e0 +5V, s\u1ee3i \u0111en l\u00e0 GND.<br \/>\n(Ph\u00eda m\u1eb7t d\u01b0\u1edbi board c\u00f3 in ch\u1eef ch\u00fa th\u00edch c\u00e1c b\u1ea1n xem k\u0129 \u0111\u1ec3 kh\u1ecfi c\u1eafm l\u1ed9n)<\/em><\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" title=\"HINHDS1307\" src=\"http:\/\/i653.photobucket.com\/albums\/uu259\/rockc4\/CLB%2013112010\/PIC%20course\/DSC01375-Copy.jpg\" alt=\"\" width=\"576\" height=\"432\" \/><\/p>\n<p><strong>&#8211; T\u00edn hi\u1ec7u:<\/strong><\/p>\n<p>+ N\u1ebfu tr\u00ean m\u1ea1ch MainPIC kh\u00f4ng c\u00f3 2 \u0111i\u1ec7n tr\u1edf k\u00e9o SCL v\u00e0 SDA l\u00ean ngu\u1ed3n th\u00ec g\u1ea1t c\u00f4ng t\u1eafc SW sang ph\u00eda c\u00f3 ch\u1eef ON (nh\u01b0 h\u00ecnh tr\u00ean). L\u01b0u \u00fd l\u00e0 c\u1ee9 g\u1ea1t c\u00f4ng t\u1eafc sang ph\u00eda n\u00e0o c\u00f3 ch\u1eef ON, c\u00f2n b\u1ea1n h\u00e0n c\u00e1i c\u00f4ng t\u1eafc theo chi\u1ec1u n\u00e0o c\u0169ng \u0111\u01b0\u1ee3c.<\/p>\n<p>Th\u1eadt ra m\u1ea1ch mainPIC t\u1eeb \u0111\u1ea7u ch\u00fang ta thi\u1ebft k\u1ebf c\u0169ng c\u00f3 2 R 4.7k, nh\u01b0ng ch\u01b0a \u0111\u01b0\u1ee3c n\u1ed1i v\u00e0o port I2C (ng\u0103n c\u00e1ch b\u1edfi c\u00e1i header 2&#215;2). V\u1eady b\u1ea1n c\u00f3 th\u1ec3 tu\u1ef3 ch\u1ecdn:<\/p>\n<p><em>*G\u1ea1t SW tr\u00ean board DS1307 sang ON.<br \/>\n*Ho\u1eb7c n\u1ed1i jumper cho port I2C tr\u00ean MainPIC.<\/em><\/p>\n<p>+ N\u1ed1i 2 d\u00e2y SDA v\u00e0 SCL tr\u00ean board DS1307 v\u1edbi 2 ch\u00e2n SDA, SCL t\u01b0\u01a1ng \u1ee9ng c\u1ee7a PIC.<br \/>\n<em>*L\u1eadt m\u1eb7t d\u01b0\u1edbi board DS1307 c\u00e1c b\u1ea1n s\u1ebd th\u1ea5y ch\u00fa th\u00edch ch\u00e2n n\u00e0o l\u00e0 SDA, ch\u00e2n n\u00e0o l\u00e0 SCL.<br \/>\n*\u0110\u1ed1i v\u1edbi PIC: SDA l\u00e0 ch\u00e2n RC4, SCL l\u00e0 ch\u00e2n RC3.<\/em><\/p>\n<p><span style=\"color: #ff0000;\"><strong>N\u00ean:<\/strong><\/span> Xo\u1eafn \u0111\u00f4i 2 d\u00e2y SDA v\u00e0 SCL l\u1ea1i v\u1edbi nhau \u0111\u1ec3 h\u1ea1n ch\u1ebf nhi\u1ec5u. Nguy\u00ean nh\u00e2n c\u00f3 th\u1ec3 g\u00e2y ra nhi\u1ec5u l\u00e0 do ch\u00fang ta l\u00e0m 2 m\u1ea1ch ri\u00eang r\u1ebd, ngu\u1ed3n k\u00e9o sang g\u00e2y nhi\u1ec5u v\u00e0 d\u00e2y t\u00edn hi\u1ec7u qu\u00e1 d\u00e0i.<br \/>\n+<strong> Ch\u00e2n CTRL:<\/strong> t\u00edn hi\u1ec7u \u0111i\u1ec1u khi\u1ec3n TRIAC<br \/>\n<em>*N\u1ed1i ch\u00e2n CTRL tr\u00ean board DS1307 v\u1edbi 1 ch\u00e2n I\/O n\u00e0o \u0111\u00f3 c\u1ee7a PIC \u0111\u1ec3 \u0111i\u1ec1u khi\u1ec3n \u0111\u00f3ng\/c\u1eaft TRIAC (trong h\u00ecnh l\u00e0 d\u00e2y m\u00e0u tr\u1eafng)<\/em><\/p>\n<p><span style=\"color: #000080;\"><strong>III. HEADER FILES<\/strong><\/span><\/p>\n<p>1. C\u00e1c h\u00e0m d\u00f9ng \u0111\u1ec3 giao ti\u1ebfp I2C \u0111\u01b0\u1ee3c defined tron<span style=\"color: #000080;\">g <span style=\"color: #008000;\"><em><strong>i2c.h<\/strong><\/em><\/span><\/span><br \/>\nSource code t\u01b0\u01a1ng \u1ee9ng \u0111\u01b0\u1ee3c vi\u1ebft trong <strong><em><span style=\"color: #008000;\">i2c.c<\/span><\/em><\/strong><\/p>\n<p><em><strong>L\u01b0u \u00fd:<\/strong><\/em><br \/>\n&#8211; Ph\u1ea3i copy 2 files n\u00e0y v\u00e0o folder ch\u1ee9a project b\u00e0i h\u1ecdc I2C.<br \/>\n&#8211; Ph\u1ea3i th\u00eam [COLOR=&#8221;royalblue&#8221;]#include &#8220;i2c.h&#8221;[\/COLOR] \u1edf \u0111\u1ea7u ch\u01b0\u01a1ng tr\u00ecnh.<br \/>\n&#8211; Add 2 files t\u01b0\u01a1ng \u1ee9ng v\u00e0o ph\u1ea7n header files v\u00e0 source files c\u1ee7a project.<br \/>\n(t\u01b0\u01a1ng t\u1ef1 nh\u01b0 d\u00f9ng c\u00e1c h\u00e0m hi\u1ec3n th\u1ecb LCD)<\/p>\n<p>2. N\u1ebfu mu\u1ed1n configure l\u1ea1i module I2C th\u00ec s\u1eeda h\u00e0m<em><span style=\"color: #008000;\"> i2c_init() <\/span><\/em>trong file <span style=\"color: #008000;\"><strong><em>i2c.c<\/em><\/strong><\/span><\/p>\n<p>3. Khi \u0111\u00e3 d\u00f9ng giao ti\u1ebfp I2C th\u00ec kh\u00f4ng \u0111\u01b0\u1ee3c d\u00f9ng 2 ch\u00e2n RC3 v\u00e0 RC4 cho m\u1ee5c \u0111\u00edch kh\u00e1c (t\u1ea5t nhi\u00ean :D). V\u00e0 2 ch\u00e2n n\u00e0y \u0111\u00e3 \u0111\u01b0\u1ee3c config trong h\u00e0m i2c_init() r\u1ed3i, n\u00ean b\u1ea1n kh\u00f4ng c\u1ea7n ph\u1ea3i c\u1ea5u h\u00ecnh cho n\u00f3 n\u1eefa.<\/p>\n<p><a href=\"http:\/\/www.mediafire.com\/?ja45zy4qq8dnnld\">Download i2c.c<\/a><\/p>\n<p><a href=\"http:\/\/www.mediafire.com\/?32d263436zi4ahb\">Download i2c.h <\/a><\/p>\n<p><span style=\"color: #000080;\"><strong>IV. B\u00c0I GI\u1ea2NG<\/strong><\/span><\/p>\n<p><strong>Ph\u1ea7n 1:<\/strong><br \/>\n<iframe loading=\"lazy\" title=\"YouTube video player\" width=\"640\" height=\"390\" src=\"http:\/\/www.youtube.com\/embed\/ST07PkatEnk\" frameborder=\"0\" allowfullscreen><\/iframe><br \/>\n(N\u1ebfu kh\u00f4ng xem \u0111\u01b0\u1ee3c clip, m\u1eddi b\u1ea1n xem t\u1ea1i \u0111\u00e2y <a href=\"http:\/\/www.youtube.com\/watch?v=ST07PkatEnk\">I2C.Part1<\/a>)<\/p>\n<p><strong>Ph\u1ea7n 2:<\/strong><br \/>\n<iframe loading=\"lazy\" title=\"YouTube video player\" width=\"640\" height=\"390\" src=\"http:\/\/www.youtube.com\/embed\/lhem9bUogkI\" frameborder=\"0\" allowfullscreen><\/iframe><br \/>\n(N\u1ebfu kh\u00f4ng xem \u0111\u01b0\u1ee3c clip, m\u1eddi b\u1ea1n xem t\u1ea1i \u0111\u00e2y <a href=\"http:\/\/www.youtube.com\/watch?v=lhem9bUogkI\">I2C.Part2<\/a>)<\/p>\n<p><strong>Ph\u1ea7n 3:<\/strong><br \/>\n<iframe loading=\"lazy\" title=\"YouTube video player\" width=\"640\" height=\"390\" src=\"http:\/\/www.youtube.com\/embed\/Ps1pOVifRrs\" frameborder=\"0\" allowfullscreen><\/iframe><br \/>\n(N\u1ebfu kh\u00f4ng xem \u0111\u01b0\u1ee3c clip, m\u1eddi b\u1ea1n xem t\u1ea1i \u0111\u00e2y <a href=\"http:\/\/www.youtube.com\/watch?v=Ps1pOVifRrs\">I2C.Part3<\/a>)<\/p>\n<p><strong>V. B\u00c0I T\u1eacP:<\/strong><br \/>\nTham kh\u1ea3o <a href=\"http:\/\/www.payitforward.edu.vn\/forum\/threads\/523\/\">b\u00e0i t\u1eadp I2C<\/a> v\u00e0 <a href=\"http:\/\/www.payitforward.edu.vn\/forum\/threads\/29\/\">th\u00f4ng tin b\u1ed5 sung.<\/a><\/p>\n<p><a href=\"http:\/\/www.payitforward.edu.vn\/forum\/forums\/23\/\">Xem th\u00eam ch\u01b0\u01a1ng tr\u00ecnh h\u1ecdc tr\u00ean forum.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>T\u00e0i li\u1ec7u n\u00e0y \u0111\u01b0\u1ee3c bi\u00ean so\u1ea1n kh\u00f4ng nh\u1eb1m v\u00e0o b\u1ea5t k\u1ef3 m\u1ee5c \u0111\u00edch n\u00e0o mang t\u00ednh th\u01b0\u01a1ng m\u1ea1i. B\u1ea1n c\u00f3 quy\u1ec1n sao ch\u00e9p, s\u1eeda ch\u1eefa v\u00e0 ph\u00e2n ph\u00e1t b\u1eb1ng b\u1ea5t k\u1ef3 h\u00ecnh th\u1ee9c n\u00e0o. H\u00e3y s\u1eed d\u1ee5ng t\u00e0i li\u1ec7u n\u00e0y, s\u1eeda ch\u1eefa, b\u1ed5 sung v\u00e0 g\u1eedi cho nh\u1eefng ai c\u1ea7n n\u00f3. Ch\u00fang t\u00f4i kh\u00f4ng \u0111\u1ec1 t\u00ean t\u00e1c gi\u1ea3 bi\u00ean so\u1ea1n. T\u00e0i li\u1ec7u n\u00e0y thu\u1ed9c quy\u1ec1n s\u1edf h\u1eefu nh\u1eefng ai \u0111ang d\u00f9ng n\u00f3. Pay it forward\u2026 B\u00e0i 6: I2C M\u1ee5c ti\u00eau H\u1ecdc l\u1eadp tr\u00ecnh giao ti\u1ebfp I2C. \u0110\u1ecdc gi\u00e1 tr\u1ecb \u0111\u1ed3ng h\u1ed3 s\u1ed1 (DS1307) (hi\u1ec3n th\u1ecb l\u00ean LCD), h\u1eb9n gi\u1edd b\u1eadt\/t\u1eaft \u2026<\/p>\n<p class=\"continue-reading-button\"> <a class=\"continue-reading-link\" href=\"https:\/\/payitforward.edu.vn\/wordpress\/tutorials\/hitech-pic\/bai-6-i2c\/\">Continue reading<i class=\"crycon-right-dir\"><\/i><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":485,"menu_order":6,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-944","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/payitforward.edu.vn\/wordpress\/wp-json\/wp\/v2\/pages\/944","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/payitforward.edu.vn\/wordpress\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/payitforward.edu.vn\/wordpress\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/payitforward.edu.vn\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/payitforward.edu.vn\/wordpress\/wp-json\/wp\/v2\/comments?post=944"}],"version-history":[{"count":10,"href":"https:\/\/payitforward.edu.vn\/wordpress\/wp-json\/wp\/v2\/pages\/944\/revisions"}],"predecessor-version":[{"id":946,"href":"https:\/\/payitforward.edu.vn\/wordpress\/wp-json\/wp\/v2\/pages\/944\/revisions\/946"}],"up":[{"embeddable":true,"href":"https:\/\/payitforward.edu.vn\/wordpress\/wp-json\/wp\/v2\/pages\/485"}],"wp:attachment":[{"href":"https:\/\/payitforward.edu.vn\/wordpress\/wp-json\/wp\/v2\/media?parent=944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}