{"id":1739,"date":"2024-06-28T02:55:19","date_gmt":"2024-06-28T02:55:19","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=1739"},"modified":"2024-08-11T17:00:50","modified_gmt":"2024-08-11T17:00:50","slug":"dc-motor-control-sound-wave","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=1739","title":{"rendered":"DC Motor Control &amp; Sound Wave"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"590\" height=\"110\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/dcc.png\" alt=\"\" class=\"wp-image-1758\" style=\"width:322px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/dcc.png 590w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/dcc-300x56.png 300w\" sizes=\"(max-width: 590px) 100vw, 590px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-1 wp-block-paragraph\" style=\"color:#5c5c5c\">Let us control a motor using Fast PWM 8bit of Timer1. In order to control a 12V dc motor we need a driver IC that L293D.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"590\" height=\"210\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/motot-L293D.png\" alt=\"\" class=\"wp-image-1742\" style=\"width:725px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/motot-L293D.png 590w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/motot-L293D-300x107.png 300w\" sizes=\"(max-width: 590px) 100vw, 590px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-2 wp-block-paragraph\" style=\"color:#5c5c5c\">In this program we use Timer1 OCR1A and OCR1B active at 8bit Fast PWM and two pin to control two dc motor. Here to control motor1 the driver has EN1 pin and we can change the direction by interchanging pin connection. Similarly to control motor2 the driver has EN2 pin and we can change the direction by interchanging pin connection. The two control pins are triggered simultaneously using INT0 external Interrupt. The control logic is-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nTCCR1A|=(1&lt;&lt;WGM10)|(1&lt;&lt;COM1B1)|(1&lt;&lt;COM1A1);\n\/* Fast PWM non inverting prescalling 256*\/\nTCCR1B|=(1&lt;&lt;WGM12)|(1&lt;&lt;CS12);\nsei();\n\/\/SREG|=(1&lt;&lt;I);\/\/Globle interrupt enable\nGICR |=(1&lt;&lt;INT0);\t\t\t\/\/External Interrupt Request 0 Enable\n\/********The rising edge of INT0 generates an interrupt request**\/\nMCUCR|=(1&lt;&lt;ISC00)|(1&lt;&lt;ISC01);\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-3 wp-block-paragraph\" style=\"color:#5c5c5c\">Parameter has been set. Now control 2 motors-<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" width=\"666\" height=\"46\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-13.jpg\" alt=\"\" class=\"wp-image-1750\" style=\"width:391px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-13.jpg 666w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-13-300x21.jpg 300w\" sizes=\"(max-width: 666px) 100vw, 666px\" \/><\/figure>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nDDRB|=(1&lt;&lt;DDB1);    \/\/OC1A Pin \nOCR1A=115;          \/\/45% duty cycle 8bit mode\nPORTB&amp;=~(1&lt;&lt;PINB3);\n_delay_ms(500);\nPORTB|=(1&lt;&lt;PINB0);\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"680\" height=\"45\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-14.jpg\" alt=\"\" class=\"wp-image-1752\" style=\"width:393px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-14.jpg 680w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-14-300x20.jpg 300w\" sizes=\"(max-width: 680px) 100vw, 680px\" \/><\/figure>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nDDRB|=(1&lt;&lt;DDB2);   \/\/OC1B Pin\nOCR1B=153;         \/\/60% duty cycle 8bit Mode\nPORTB|=(1&lt;&lt;PINB3); \n_delay_ms(500);\nPORTB&amp;=~(1&lt;&lt;PINB0);\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-4 wp-block-paragraph\" style=\"color:#5c5c5c\">Download the int0.c and int0.h file, the main program is as follow-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n #include&lt;avr\/io.h&gt;\n #include&lt;util\/delay.h&gt;\n #include&lt;avr\/interrupt.h&gt;\n #include&quot;int0.h&quot;\nuint16_t sample=1;\nint main(void)\n {\n   DDRB|=(1&lt;&lt;DDB0)|(1&lt;&lt;DDB3);\/\/Control PIN\n   int_int0();               \/\/External Interrupt enable\n   timer1();                 \/\/Timer1 Enable\n  while(1)\n    {\n        sample ? select2(): select1();\n    }\n  return 0;\n }\nISR(INT0_vect)\n{\n  sample^=0x01;\n}\n<\/pre><\/div>\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\">\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-3e41869c wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/DC-motor-Control.rar\" style=\"padding-top:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--40)\">download<\/a><\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-5 wp-block-paragraph\" style=\"color:#252525\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/DC-motor-Control.rar\">DC motor control.rar<\/a><\/p>\n<\/div>\n<\/div>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"590\" height=\"110\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/sounds-1.png\" alt=\"\" class=\"wp-image-1760\" style=\"width:338px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/sounds-1.png 590w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/sounds-1-300x56.png 300w\" sizes=\"(max-width: 590px) 100vw, 590px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-6 wp-block-paragraph\" style=\"color:#5c5c5c\">In AVR we can also generate sound using Timer. In this program we use an 8\u03a9 speaker to generate sound which is connected to OC2 pin. Since OC2 pin of Timer2 can generate waveform and sound is nothing but a waveform. The Timer should operate in Fast PWM non-inverting mode with no prescalling and F_CPU=16HHz. The OCR2(duty cycle register) register\u2019s value vary with the value stored in the flash memory. The main program as follow with ATmega8-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n\/***********************************************************************************************************************************\n************************************************************************************************************************************\n********************************* Sound Generation using PWM *************************************************************\n************************************************************************************************************************************\n\t\tSubeer Kumar Sarkar\n\t\tElectrical &amp; Electronic Engineer\n************************************************************************************************************************************\n************************************************************************************************************************************\n***********************************************************************************************************************************\/\n#include&lt;avr\/io.h&gt;\n#include&lt;util\/delay.h&gt;\n#include&lt;avr\/pgmspace.h&gt;\n\/************************** Sound Generation *****************************************\/\nconst uint8_t sine&#x5B;256]PROGMEM={\n\t\t\t\t\t\t\t\t127,130,133,136,139,143,146,149,152,155,158,161,\n\t\t\t\t\t\t\t\t164,167,170,173,176,178,181,184,187,189,192,195,197,200,203,205,\n\t\t\t\t\t\t\t\t207,210,212,214,217,219,221,223,225,227,229,231,232,234,236,237,\n\t\t\t\t\t\t\t\t239,240,242,243,244,245,246,248,248,249,250,251,251,252,253,253,\n\t\t\t\t\t\t\t\t253,254,254,254,254,254,254,254,253,253,253,252,252,251,250,250,\n\t\t\t\t\t\t\t\t249,248,247,246,245,243,242,241,239,238,236,235,233,231,229,227,\n\t\t\t\t\t\t\t\t225,224,221,219,217,215,213,210,208,206,203,201,198,195,193,190,\n\t\t\t\t\t\t\t\t187,185,182,179,176,173,170,167,164,161,158,155,152,149,146,143,\n\t\t\t\t\t\t\t\t140,137,134,131,128,125,121,118,115,112,109,106,103,100,97,94,91,\n\t\t\t\t\t\t\t\t88,85,82,79,76,73,71,68,65,62,60,57,55,52,50,47,45,42,40,38,36,34,\n\t\t\t\t\t\t\t\t31,29,27,26,24,22,20,19,17,15,14,13,11,10,9,8,7,6,5,4,3,3,2,2,1,\n\t\t\t\t\t\t\t\t1,0,0,0,0,0,0,0,1,1,1,2,2,3,4,4,5,6,7,8,9,10,12,13,14,16,17,19,\n\t\t\t\t\t\t\t\t21,22,24,26,28,30,32,34,36,39,41,43,45,48,50,53,55,58,61,63,66,\n\t\t\t\t\t\t\t\t69,72,74,77,80,83,86,89,92,95,98,101,104,107,110,113,116,119,122\n\t\t\t\t\t\t\t    };\n\/**********************************************************************************************\/\nuint8_t i;\nint main(void)\n{\n\/********************************Output PIN******************************************\/\nDDRB|=(1&lt;&lt;DDB3);\t\t\t\t\t\t\/\/oc2 pin connected\n\/****Timer Clock = CPU Clock (No Prescalling)  Mode = Fast PWM    PWM Output = Non Inverted******\/\nTCCR2|=(1&lt;&lt;WGM21)|(1&lt;&lt;WGM20)|(1&lt;&lt;COM21)|(1&lt;&lt;CS20);\n\/**********************************************************************************************\/\nwhile(1)\n{\nuint8_t delay,n;\n\/*******************************Control program**************************************\/\n\tfor(delay=1;delay&lt;=50;delay++)\n\t{\n\t\tfor(n=0;n&lt;(51-delay);n++)\n\t\t{\n\t\t\tfor(i=0;i&lt;=254;i++)\n\t\t\t{\n\t\t\t\tOCR2=pgm_read_byte(&amp;sine&#x5B;i]);\n\t\t\t\t_delay_loop_2(delay);\n\t\t\t}\n\t\t}\n\t}\n\tfor(delay=50;delay&gt;=2;delay--)\n\t\t{\n\t\tfor(n=0;n&lt;(51-delay);n++)\n\t\t\t{\n\t\t\tfor(i=0;i&lt;=254;i++)\n\t\t\t{\n\t\t\t\tOCR2=pgm_read_byte(&amp;sine&#x5B;i]);\n\t\t\t\t_delay_loop_2(delay);\n\t\t\t}\n\t\t}\n\t}\n\/************************************************************************************\/\n}\nreturn 0;\n}\n\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-7 wp-block-paragraph\" style=\"color:#5c5c5c\">For 16HHz crystal setting The FUSE bit as follow-<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-8 wp-block-paragraph\" style=\"color:#252525\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>HFUSE=0xc9<\/strong>  &amp; <strong> LFUSE=0xEF<\/strong><\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\">\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-3e41869c wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/Sound-Wave-Genaration.rar\" style=\"padding-top:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--40)\">download <\/a><\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-9 wp-block-paragraph\" style=\"color:#252525\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/Sound-Wave-Genaration.rar\">Sound generation.rar<\/a><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Let us control a motor using Fast PWM 8bit of Timer1. In order to control a 12V dc motor we need a driver IC that L293D. In this program we use Timer1 OCR1A and OCR1B active at 8bit Fast PWM and two pin to control two dc motor. Here to control motor1 the driver has [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-1739","post","type-post","status-publish","format-standard","hentry","category-pwm"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1739","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1739"}],"version-history":[{"count":16,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1739\/revisions"}],"predecessor-version":[{"id":2611,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1739\/revisions\/2611"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}