{"id":1356,"date":"2020-09-18T22:46:47","date_gmt":"2020-09-19T03:46:47","guid":{"rendered":"http:\/\/abrammorphew.com\/notes\/?p=1356"},"modified":"2024-06-19T19:11:56","modified_gmt":"2024-06-20T00:11:56","slug":"200-khz-arduino-clock-generator","status":"publish","type":"post","link":"http:\/\/abrammorphew.com\/notes\/2020\/09\/18\/200-khz-arduino-clock-generator\/","title":{"rendered":"200 kHz Arduino Clock Generator"},"content":{"rendered":"\n<p>Someone contacted me recently about using an ATMega328 to generate a 200 kHz clock signal for a BBD analog delay chip. I finally had a few minutes today to sit down and ensure that this code works. Varying the OCR0A value acts as a frequency adjustment on the output following the formula: f = 16e6 \/ (4 * OCR0A) where OCR0A != 0. With a value of 0, the output frequency is roughly 8 MHz. It seemed fairly stable enough at this frequency, but I imagine that it would start having issues with more instructions. Either way, it makes for a very usable clock signal in the kHz range. I&#8217;ll try it out on a BBD hopefully one day myself and see.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:false,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;darcula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">\/\/ ===== 200 kHz Clock Signal Generator ==== \/\/\nint  pin = 6;\nbyte data = LOW;\n\nvoid setup() {\n  setupTimer();\n  pinMode(pin, OUTPUT);\n                 \/\/ f = 16e6 \/ (4 * OCR0A)\n  OCR0A = 20;    \/\/ varies CLK frequency: 0 =&amp;gt; 8 MHz, 255 =&amp;gt; 16 kHz\n  digitalWrite(pin,data);\n}\n\nvoid loop() {\n\n}\n\nvoid setupTimer() {\n  cli(); \n \/*--- TIMER0 CONFIG ---*\/  \n  TCCR0A = 0b11000001;\n  TCCR0B = 0b00001001;    \/\/ last 3 bits set prescalar for Timer0\n  TIMSK0 = 0b00000010;    \/\/ set OCIE0A high\n  TIFR0  = 0b00000010;    \/\/ set OCF0A high\n  sei(); \n}\n\nISR(TIMER0_COMPA_vect) {\n  data = !data;\n  digitalWrite(pin, data);\n}\n&lt;\/pre&gt;<\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"480\" src=\"http:\/\/abrammorphew.com\/notes\/wp-content\/uploads\/2020\/09\/arduino_200kHz.jpg\" alt=\"\" class=\"wp-image-1425\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Someone contacted me recently about using an ATMega328 to generate a 200 kHz clock signal for a BBD analog delay chip. I finally had a few minutes today to sit down and ensure that this code works. Varying the OCR0A value acts as a frequency adjustment on the output following the formula: f = 16e6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1425,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[102,108],"class_list":["post-1356","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-notes","tag-arduino","tag-digital"],"_links":{"self":[{"href":"http:\/\/abrammorphew.com\/notes\/wp-json\/wp\/v2\/posts\/1356","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/abrammorphew.com\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/abrammorphew.com\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/abrammorphew.com\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/abrammorphew.com\/notes\/wp-json\/wp\/v2\/comments?post=1356"}],"version-history":[{"count":5,"href":"http:\/\/abrammorphew.com\/notes\/wp-json\/wp\/v2\/posts\/1356\/revisions"}],"predecessor-version":[{"id":1480,"href":"http:\/\/abrammorphew.com\/notes\/wp-json\/wp\/v2\/posts\/1356\/revisions\/1480"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/abrammorphew.com\/notes\/wp-json\/wp\/v2\/media\/1425"}],"wp:attachment":[{"href":"http:\/\/abrammorphew.com\/notes\/wp-json\/wp\/v2\/media?parent=1356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/abrammorphew.com\/notes\/wp-json\/wp\/v2\/categories?post=1356"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/abrammorphew.com\/notes\/wp-json\/wp\/v2\/tags?post=1356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}