วันอังคารที่ 31 ตุลาคม พ.ศ. 2560

สิ่งที่ใครใครไม่อาจจะชนะได้ผู้ปฏิบัติที่แท้จริงนั้น ไม่จำเป็นต้องคำนึงถึงชาติหน้า-ชาติหลัง หรือ นรก-สวรรค์อะไรก็ได้ ขอให้ตั้งใจปฏิบัติให้ตรง ศีล สมาธิ ปัญญา อย่างแน่วแน่ก็พอ ถ้าสวรรค์มีจริงถึง 16 ชั้น ตามตำรา ผู้ปฏิบัติดีแล้ว ก็ย่อมได้เลื่อนฐานะของตนเองตามลำดับ หรือถ้า สวรรค์-นิพพานไม่มีเลย ผู้ปฏิบัติดีในขณะนี้ย่อมไม่ไร้ประโยชน์ ย่อมอยู่เป็นสุข เป็นมนุษย์ชั้นเลิศ " "จิตที่ส่งออกนอก เป็นสมุทัย ผลอันเกิดจากจิตที่ส่งออกนอก เป็นทุกข์ จิตเห็นจิตอย่างแจ่มแจ้ง เป็นมรรค ผลอันเกิดจากจิตเห็นจิต เป็นนิโรธ คิดเท่าไรๆ ก็ไม่รู้ ต่อเมื่อหยุดได้จึงรู้ แต่ต้องอาศัยความคิดนั้นแหละจึงรู้

ความสั่งสมซึ่งบุญทำให้เกิดสุขความสั่งสมซึ่งบุญทำให้เกิดสุข Sompong Tungmepol Sompong Tungmepol ติดตามแล้ว44K เพิ่มลงใน แชร์ เพิ่มเติม ดู 2,232 ครั้ง 10 0 เผยแพร่เมื่อ 20 ก.ย. 2012 ความสั่งสมซึ่งบุญทำให้เกิดสุขสำหรับบุคคลที่ตื่นอยู่ตลอดเวลา หมั่นศึกษาไตรสิกขาทั้งกลางวันและกลางคืน น้อมจิตเข้าหานิพพาน อาสวะทั้งหลายย่อมตั้งอยู่ไม่ได้'ปาฏิหาริย์แห่งการตื่น' จะเกิดขึ้นได้เมื่อเราตื่นจากฝัน ธรรมชาติแห่งพุทธภาวะ หมายถึง ธรรมชาติอันหนึ่ง ซึ่งทำสัตว์ให้เข้าถึงพุทธภาวะ ... "พุทธภาวะ" อันหมายถึงภาวะแห่งความรู้ ความตื่น ความเบิกบาน สิ่งที่เรียกว่า "ความรู้" ในที่นี้ ...จนเข้าถึงสภาวะพลังงานอันสูงสุด อันเป็นหนึ่งเดียว ของรูป-นาม เมื่อรูป นาม ดับสลาย เข้าสู่สภาวะพลังงานตื่นอันเป็นที่สุด อันเป็นปรมัตถปัญญา - ปัญญาวิมุตติ คือภาวะแห่งพุทธะ หมวดหมู่ แนวปฏิบัติและการใช้ชีวิต สัญญาอนุญาต สัญญาอนุญาตมาตรฐานของ YouTube แสดงน้อยลง ความคิดเห็น • 4 สมพงศ์ อินดัสเตรียล อิเล็กทรอนิคส์ เพิ่มความคิดเห็นสาธารณะ... ความคิดเห็นยอดนิยม Sompong Tungmepol Sompong Tungmepol3 ปีที่ผ่านมา ดูกรภิกษุทั้งหลาย ถ้าแม้บัณฑิตผู้ฉลาด มีปัญญาใคร่ครวญ อายตนะภายใน ๖ นั้น ทางตา หู จมูก ลิ้น กาย ใจ ก็จะปรากฏว่าเป็นของ ว่าง เปล่า สูญทั้งนั้น ฯ คำว่า โจรผู้ฆ่าชาวบ้านนั้น เป็นชื่อแห่งอายตนะภายนอก ๖ ตาย่อม เดือดร้อนเพราะรูปเป็นที่พอใจและไม่พอใจ หูย่อมเดือดร้อน เพราะเสียงเป็นที่ พอใจและไม่พอใจ จมูกย่อมเดือดร้อนเพราะกลิ่นเป็นที่พอใจและไม่พอใจ ลิ้น ย่อมเดือดร้อนเพราะรสเป็นที่พอใจและไม่พอใจ กายย่อมเดือดร้อนเพราะโผฏ- *ฐัพพะเป็นที่พอใจและไม่พอใจ ใจย่อมเดือดร้อนเพราะธรรมารมณ์เป็นที่พอใจและ ไม่พอใจ คำว่า ห้วงน้ำใหญ่นั้น เป็นชื่อแห่งโอฆะทั้ง ๔ คือ กาโมฆะ ภโวฆะ ทิฏโฐฆะ อวิชโชฆะ คำว่า ฝั่งข้างนี้อันเป็นที่น่ารังเกียจ เต็มไปด้วยภัยอันตราย นั้น เป็นชื่อแห่งร่างกายของตน คำว่า ฝั่งข้างโน้นเป็นที่เกษม ปลอดภัยนั้น เป็นชื่อแห่งนิพพาน คำว่า แพนั้น เป็นชื่อแห่งอริยมรรคมีองค์ ๘ ประการ คือ สัมมาทิฐิ ฯลฯ สัมมาสมาธิ คำว่า พยายามข้ามไปด้วยมือและเท้า เป็นชื่อแห่ง วิริยารัมภะ คำว่า ผู้เป็นพราหมณ์ว่ายข้ามฟากถึงฝั่งโน้นแล้วขึ้นบกไป เป็นชื่อ แห่งพระอรหันต์ ฯ อ่านเพิ่มเติม ตอบกลับ 1 ดูการตอบกลับ Sompong Tungmepol Sompong Tungmepol1 ปีที่ผ่านมา เราพากันมาสำรวมจิตกันเถิด จิตนี้พวกเราเคยปล่อยให้มันเสาะแสวงหาสิ่งที่มันชอบใจมานานแล้ว และสิ่งนั้นมันก็เคยได้มาแล้ว แต่จิตมันก็ไม่พอสักทีหาอยู่ร่ำไป เมื่อไรมันจะรู้จักจบจักพอกันสักที คนที่อยู่รอบๆ ตัวของเรานี้ เราก็เห็นตำตาอยู่แล้ว เมื่อตายไปก็ไม่เห็นเอาอะไรไปด้วย แม้แต่ร่างกายอันนี้ ก็ทอดทิ้งถมพื้นแผ่นดินด้วยกันทั้งนั้น แต่จิตที่อยู่นิ่งไม่แส่ส่ายแสวงหาอะไรทั้งหมด ตั้งมั่นอยู่กับพุทโธอันเดียว เรายังไม่เคยได้เลย จงพากันมาภาวนาพุทโธๆ ให้จิตมันหยุดนิ่งอยู่อันเดียว ลองดูซิ บางที จิตที่อยู่กับพุทโธอันเดียว กลับจะได้มากกว่า และเป็นของแปลกประหลาดกว่าที่เป็นมาแล้ว ทุกสิ่งทุกอย่างมันออกจากหนึ่ง สิ่งอันหนึ่งทั้งนั้น เช่น การนับก็ตั้งต้นจากหนึ่งก่อน หนึ่งสองหนก็เป็นสอง หนึ่งสามหนก็เป็นสาม ดังนี้เป็นต้น หรือต้นหมากรากไม้ทั้งปวงก็ออกมาจากหนึ่งทั้งนั้น (คือ รากของมัน ) คนเราก็เหมือนกันเมื่อเกิดมาทีแรก ก็เกิดจากปฏิสนธิจิตดวงเดียวแท้ๆ เมื่อคลอดออกมาแล้วมีอายตนะ ขันธ์ห้า เครื่องใช้หลายอย่าง จิตก็ปรุงแต่งไปหลายอย่างหลายอัน จนนับจิตไม่ถ้วน ไม่ทราบว่าจิตมีกี่ดวง ยุ่งกันไปหมด จิตเดิมแท้เลยไม่เห็น ฉะนั้น พระพุทธเจ้าจึงสอนให้ค้นหา จิต คือ ภาวนา พุทโธ เอาจิตมารวมอยู่ในพุทโธอันเดียว จึงจะเห็นจิต การค้นหาจิตเราจะต้องทำภาวนาพุทโธ ทำให้จิตรวมเป็นอันหนึ่งอันเดียวกันจึงจะเห็นจิตของตน เมื่อเราค้นหาจิตเห็นจิตแล้ว และเห็นว่านี่คือต้นตอของจิต ของสิ่งทั้งปวงที่มีอยู่ในโลกนี้ จิตที่มากหลายอย่างนั้น มันออกไปจากจิตอันเดียวนี้ เมื่อจิตอยู่นิ่งกับพุทโธอันเดียว สรรพกิเลสทั้งหลายก็ไม่มี แล้วจงใช้สติประคองจิตอันนั้นให้นิ่งอยู่กับพุทโธอันเดียวเสียก่อน อย่าให้ส่งส่ายไปมาทุกอริยาบถทั้งสี่ ทำให้ชำนิชำนาญคล่องแคล่ว จนเราจะให้อยู่ก็ได้ หรือเราจะให้คิดค้นพิจารณาในธรรมต่างๆ ก็ได้ หรือคิดค้นธรรมต่างๆ แล้วจะให้มานิ่งอยู่กับใจก็ได้ เมื่อเข้าใจถึงแล้วพุทโธไม่ต้องบริกรรมก็ได้ อ่านเพิ่มเติม ตอบกลับ Ladda Ladda4 ปีที่ผ่านมา อนุโมทนาสาธุ ตอบกลับ เล่นอัตโนมัติ รายการถัดไป ผู้มุ่งประกอบธรรมเครื่องสงบใจ Sompong Tungmepol ดู 8,887 ครั้ง 31:08 ธรรมที่ควรตั้งไว้ในตน Sompong Tungmepol ขอแนะนำสำหรับคุณ 29:52 จิต เกิด แล้ว ดับ Sompong Tungmepol ขอแนะนำสำหรับคุณ 1:11:48 หลวงพ่อฤาษีการหลุดพ้นจากกิเลสเป็นสมุทเฉทประหารด้วยวิปัสสนาญาณ ๙ Sompong Tungmepol ขอแนะนำสำหรับคุณ 1:00:18 จิตหลุดพ้นจากอาสวะ Sompong Tungmepol ดู 2,273 ครั้ง 31:05 ทางสิ้นทุกข์ สิ้นตัณหา Sompong Tungmepol ขอแนะนำสำหรับคุณ 25:20 การเข้าถึงธรรมที่พระพุุทธเจ้าทรงสอน Sompong Tungmepol ดู 2,530 ครั้ง 9:51 บุคคลรู้แจ้งในธรรมที่พระสัมมาสัมพุทธเจ้าทรงแสดงแล้ว จากผู้ใด พึงนอบน้อมผู้นั้นโดยเคารพ Sompong Tungmepol ขอแนะนำสำหรับคุณ 30:16 หลวงพ่อฤาษีลิงดำ เรื่อง ฟังธรรมขณะหลับ Buddhism Channel ดู 61,303 ครั้ง 2:18:39 วิธีฝึกสติในชีวิตประจำวัน Sompong Tungmepol ดู 9,120 ครั้ง 11:14 จิตภาวนาในพระศาสนาของพระสัมมาสัมพุทธเจ้า Sompong Tungmepol ดู 2,086 ครั้ง 41:19 ผู้สดุ้งกลัวบาปเพียรเผากิเลส Sompong Tungmepol ดู 786 ครั้ง 32:01 วิธีสร้างความผ่องใสให้กับจิตใจ Sompong Tungmepol ดู 400,397 ครั้ง 26:28 ผู้มีอัธยาศัยอนาคามีผล Sompong Tungmepol ดู 2,639 ครั้ง 8:25 ผู้ไม่เศร้าโศกไม่มีทุกข์เวทนาทางใจ Sompong Tungmepol ดู 1,665 ครั้ง 13:23 ฉลาดในการรักษาจิตของเรา Sompong Tungmepol ดู 4,893 ครั้ง 37:05 คนบุญน้อยย่อมมีความสุขน้อย.wmv JchaiJane ดู 10,987 ครั้ง 50:50 วิธีเจริญวิปัสสนากรรมฐานที่ถูกต้อง Sompong Tungmepol ขอแนะนำสำหรับคุณ 25:14 อริยสัจสี่ของคนธรรมดาสามัญ Sompong Tungmepol ขอแนะนำสำหรับคุณ การพิจารณาร่างกาย Sompong Tungmepol ดู 48,545 ครั้ง

ความสั่งสมซึ่งบุญทำให้เกิดสุขความสั่งสมซึ่งบุญทำให้เกิดสุข Sompong Tungmepol Sompong Tungmepol ติดตามแล้ว44K เพิ่มลงใน แชร์ เพิ่มเติม ดู 2,232 ครั้ง 10 0 เผยแพร่เมื่อ 20 ก.ย. 2012 ความสั่งสมซึ่งบุญทำให้เกิดสุขสำหรับบุคคลที่ตื่นอยู่ตลอดเวลา หมั่นศึกษาไตรสิกขาทั้งกลางวันและกลางคืน น้อมจิตเข้าหานิพพาน อาสวะทั้งหลายย่อมตั้งอยู่ไม่ได้'ปาฏิหาริย์แห่งการตื่น' จะเกิดขึ้นได้เมื่อเราตื่นจากฝัน ธรรมชาติแห่งพุทธภาวะ หมายถึง ธรรมชาติอันหนึ่ง ซึ่งทำสัตว์ให้เข้าถึงพุทธภาวะ ... "พุทธภาวะ" อันหมายถึงภาวะแห่งความรู้ ความตื่น ความเบิกบาน สิ่งที่เรียกว่า "ความรู้" ในที่นี้ ...จนเข้าถึงสภาวะพลังงานอันสูงสุด อันเป็นหนึ่งเดียว ของรูป-นาม เมื่อรูป นาม ดับสลาย เข้าสู่สภาวะพลังงานตื่นอันเป็นที่สุด อันเป็นปรมัตถปัญญา - ปัญญาวิมุตติ คือภาวะแห่งพุทธะ หมวดหมู่ แนวปฏิบัติและการใช้ชีวิต สัญญาอนุญาต สัญญาอนุญาตมาตรฐานของ YouTube แสดงน้อยลง ความคิดเห็น • 4 สมพงศ์ อินดัสเตรียล อิเล็กทรอนิคส์ เพิ่มความคิดเห็นสาธารณะ... ความคิดเห็นยอดนิยม Sompong Tungmepol Sompong Tungmepol3 ปีที่ผ่านมา ดูกรภิกษุทั้งหลาย ถ้าแม้บัณฑิตผู้ฉลาด มีปัญญาใคร่ครวญ อายตนะภายใน ๖ นั้น ทางตา หู จมูก ลิ้น กาย ใจ ก็จะปรากฏว่าเป็นของ ว่าง เปล่า สูญทั้งนั้น ฯ คำว่า โจรผู้ฆ่าชาวบ้านนั้น เป็นชื่อแห่งอายตนะภายนอก ๖ ตาย่อม เดือดร้อนเพราะรูปเป็นที่พอใจและไม่พอใจ หูย่อมเดือดร้อน เพราะเสียงเป็นที่ พอใจและไม่พอใจ จมูกย่อมเดือดร้อนเพราะกลิ่นเป็นที่พอใจและไม่พอใจ ลิ้น ย่อมเดือดร้อนเพราะรสเป็นที่พอใจและไม่พอใจ กายย่อมเดือดร้อนเพราะโผฏ- *ฐัพพะเป็นที่พอใจและไม่พอใจ ใจย่อมเดือดร้อนเพราะธรรมารมณ์เป็นที่พอใจและ ไม่พอใจ คำว่า ห้วงน้ำใหญ่นั้น เป็นชื่อแห่งโอฆะทั้ง ๔ คือ กาโมฆะ ภโวฆะ ทิฏโฐฆะ อวิชโชฆะ คำว่า ฝั่งข้างนี้อันเป็นที่น่ารังเกียจ เต็มไปด้วยภัยอันตราย นั้น เป็นชื่อแห่งร่างกายของตน คำว่า ฝั่งข้างโน้นเป็นที่เกษม ปลอดภัยนั้น เป็นชื่อแห่งนิพพาน คำว่า แพนั้น เป็นชื่อแห่งอริยมรรคมีองค์ ๘ ประการ คือ สัมมาทิฐิ ฯลฯ สัมมาสมาธิ คำว่า พยายามข้ามไปด้วยมือและเท้า เป็นชื่อแห่ง วิริยารัมภะ คำว่า ผู้เป็นพราหมณ์ว่ายข้ามฟากถึงฝั่งโน้นแล้วขึ้นบกไป เป็นชื่อ แห่งพระอรหันต์ ฯ อ่านเพิ่มเติม ตอบกลับ 1 ดูการตอบกลับ Sompong Tungmepol Sompong Tungmepol1 ปีที่ผ่านมา เราพากันมาสำรวมจิตกันเถิด จิตนี้พวกเราเคยปล่อยให้มันเสาะแสวงหาสิ่งที่มันชอบใจมานานแล้ว และสิ่งนั้นมันก็เคยได้มาแล้ว แต่จิตมันก็ไม่พอสักทีหาอยู่ร่ำไป เมื่อไรมันจะรู้จักจบจักพอกันสักที คนที่อยู่รอบๆ ตัวของเรานี้ เราก็เห็นตำตาอยู่แล้ว เมื่อตายไปก็ไม่เห็นเอาอะไรไปด้วย แม้แต่ร่างกายอันนี้ ก็ทอดทิ้งถมพื้นแผ่นดินด้วยกันทั้งนั้น แต่จิตที่อยู่นิ่งไม่แส่ส่ายแสวงหาอะไรทั้งหมด ตั้งมั่นอยู่กับพุทโธอันเดียว เรายังไม่เคยได้เลย จงพากันมาภาวนาพุทโธๆ ให้จิตมันหยุดนิ่งอยู่อันเดียว ลองดูซิ บางที จิตที่อยู่กับพุทโธอันเดียว กลับจะได้มากกว่า และเป็นของแปลกประหลาดกว่าที่เป็นมาแล้ว ทุกสิ่งทุกอย่างมันออกจากหนึ่ง สิ่งอันหนึ่งทั้งนั้น เช่น การนับก็ตั้งต้นจากหนึ่งก่อน หนึ่งสองหนก็เป็นสอง หนึ่งสามหนก็เป็นสาม ดังนี้เป็นต้น หรือต้นหมากรากไม้ทั้งปวงก็ออกมาจากหนึ่งทั้งนั้น (คือ รากของมัน ) คนเราก็เหมือนกันเมื่อเกิดมาทีแรก ก็เกิดจากปฏิสนธิจิตดวงเดียวแท้ๆ เมื่อคลอดออกมาแล้วมีอายตนะ ขันธ์ห้า เครื่องใช้หลายอย่าง จิตก็ปรุงแต่งไปหลายอย่างหลายอัน จนนับจิตไม่ถ้วน ไม่ทราบว่าจิตมีกี่ดวง ยุ่งกันไปหมด จิตเดิมแท้เลยไม่เห็น ฉะนั้น พระพุทธเจ้าจึงสอนให้ค้นหา จิต คือ ภาวนา พุทโธ เอาจิตมารวมอยู่ในพุทโธอันเดียว จึงจะเห็นจิต การค้นหาจิตเราจะต้องทำภาวนาพุทโธ ทำให้จิตรวมเป็นอันหนึ่งอันเดียวกันจึงจะเห็นจิตของตน เมื่อเราค้นหาจิตเห็นจิตแล้ว และเห็นว่านี่คือต้นตอของจิต ของสิ่งทั้งปวงที่มีอยู่ในโลกนี้ จิตที่มากหลายอย่างนั้น มันออกไปจากจิตอันเดียวนี้ เมื่อจิตอยู่นิ่งกับพุทโธอันเดียว สรรพกิเลสทั้งหลายก็ไม่มี แล้วจงใช้สติประคองจิตอันนั้นให้นิ่งอยู่กับพุทโธอันเดียวเสียก่อน อย่าให้ส่งส่ายไปมาทุกอริยาบถทั้งสี่ ทำให้ชำนิชำนาญคล่องแคล่ว จนเราจะให้อยู่ก็ได้ หรือเราจะให้คิดค้นพิจารณาในธรรมต่างๆ ก็ได้ หรือคิดค้นธรรมต่างๆ แล้วจะให้มานิ่งอยู่กับใจก็ได้ เมื่อเข้าใจถึงแล้วพุทโธไม่ต้องบริกรรมก็ได้ อ่านเพิ่มเติม ตอบกลับ Ladda Ladda4 ปีที่ผ่านมา อนุโมทนาสาธุ ตอบกลับ เล่นอัตโนมัติ รายการถัดไป ผู้มุ่งประกอบธรรมเครื่องสงบใจ Sompong Tungmepol ดู 8,887 ครั้ง 31:08 ธรรมที่ควรตั้งไว้ในตน Sompong Tungmepol ขอแนะนำสำหรับคุณ 29:52 จิต เกิด แล้ว ดับ Sompong Tungmepol ขอแนะนำสำหรับคุณ 1:11:48 หลวงพ่อฤาษีการหลุดพ้นจากกิเลสเป็นสมุทเฉทประหารด้วยวิปัสสนาญาณ ๙ Sompong Tungmepol ขอแนะนำสำหรับคุณ 1:00:18 จิตหลุดพ้นจากอาสวะ Sompong Tungmepol ดู 2,273 ครั้ง 31:05 ทางสิ้นทุกข์ สิ้นตัณหา Sompong Tungmepol ขอแนะนำสำหรับคุณ 25:20 การเข้าถึงธรรมที่พระพุุทธเจ้าทรงสอน Sompong Tungmepol ดู 2,530 ครั้ง 9:51 บุคคลรู้แจ้งในธรรมที่พระสัมมาสัมพุทธเจ้าทรงแสดงแล้ว จากผู้ใด พึงนอบน้อมผู้นั้นโดยเคารพ Sompong Tungmepol ขอแนะนำสำหรับคุณ 30:16 หลวงพ่อฤาษีลิงดำ เรื่อง ฟังธรรมขณะหลับ Buddhism Channel ดู 61,303 ครั้ง 2:18:39 วิธีฝึกสติในชีวิตประจำวัน Sompong Tungmepol ดู 9,120 ครั้ง 11:14 จิตภาวนาในพระศาสนาของพระสัมมาสัมพุทธเจ้า Sompong Tungmepol ดู 2,086 ครั้ง 41:19 ผู้สดุ้งกลัวบาปเพียรเผากิเลส Sompong Tungmepol ดู 786 ครั้ง 32:01 วิธีสร้างความผ่องใสให้กับจิตใจ Sompong Tungmepol ดู 400,397 ครั้ง 26:28 ผู้มีอัธยาศัยอนาคามีผล Sompong Tungmepol ดู 2,639 ครั้ง 8:25 ผู้ไม่เศร้าโศกไม่มีทุกข์เวทนาทางใจ Sompong Tungmepol ดู 1,665 ครั้ง 13:23 ฉลาดในการรักษาจิตของเรา Sompong Tungmepol ดู 4,893 ครั้ง 37:05 คนบุญน้อยย่อมมีความสุขน้อย.wmv JchaiJane ดู 10,987 ครั้ง 50:50 วิธีเจริญวิปัสสนากรรมฐานที่ถูกต้อง Sompong Tungmepol ขอแนะนำสำหรับคุณ 25:14 อริยสัจสี่ของคนธรรมดาสามัญ Sompong Tungmepol ขอแนะนำสำหรับคุณ การพิจารณาร่างกาย Sompong Tungmepol ดู 48,545 ครั้ง

arduino thailand

// DDS Sine Generator 3 phase ATMEGA 168 328P PWM 4KHZ + Danijel Gorupec, 2015

// Support 7 Segments Show Hz  15/07/2017
// Import SevSeg Library : https://playground.arduino.cc/Main/SevenSegmentLibrary

#include "arduino.h" //Store data in flash (program) memory instead of SRAM
#include "avr/pgmspace.h"
#include "avr/io.h"
#include "SevSeg.h"
SevSeg sevseg; //Instantiate a seven segment controller object

   const byte sine256[] PROGMEM  = {
  127,130,133,136,139,143,146,149,152,155,158,161,164,167,170,173,176,178,181,184,187,190,192,195,198,200,203,205,208,210,212,215,217,219,221,223,225,227,229,231,233,234,236,238,239,240,
  242,243,244,245,247,248,249,249,250,251,252,252,253,253,253,254,254,254,254,254,254,254,253,253,253,252,252,251,250,249,249,248,247,245,244,243,242,240,239,238,236,234,233,231,229,227,225,223,
  221,219,217,215,212,210,208,205,203,200,198,195,192,190,187,184,181,178,176,173,170,167,164,161,158,155,152,149,146,143,139,136,133,130,127,124,121,118,115,111,108,105,102,99,96,93,90,87,84,81,78,
  76,73,70,67,64,62,59,56,54,51,49,46,44,42,39,37,35,33,31,29,27,25,23,21,20,18,16,15,14,12,11,10,9,7,6,5,5,4,3,2,2,1,1,1,0,0,0,0,0,0,0,1,1,1,2,2,3,4,5,5,6,7,9,10,11,12,14,15,16,18,20,21,23,25,27,29,31,
  33,35,37,39,42,44,46,49,51,54,56,59,62,64,67,70,73,76,78,81,84,87,90,93,96,99,102,105,108,111,115,118,121,124

};
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) //define a bit to have the properties of a clear bit operator
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))//define a bit to have the properties of a set bit operator
#define INPUT_DIR ((PINC&0x04)==0)  // 00000010 = A2 //Control Direction


int PWM1=  9;  //PWM1 output, phase 1
int PWM2 = 10; //PWM2 output, phase 2
int PWM3 = 11; //PWM3 output, phase 3


//int offset_1 = 85; //offset 1 is 120 degrees out of phase with previous phase, Refer to PWM to sine.xls
//int offset_2 = 170; //offset 2 is 120 degrees out of phase with offset 1. Refer to PWM to sine.xls
int offset_1; //offset 1 is 120 degrees out of phase with previous phase, Refer to PWM to sine.xls
int offset_2; //offset 2 is 120 degrees out of phase with offset 1. Refer to PWM to sine.xls
//int program_exec_time = A3; //monitor how quickly the interrupt trigger
int ISR_exec_time = A0; //monitor how long the interrupt takes
int INVERTOR_ENABLE = A1; //INVERTOR ENABLE

double ad_cel; //Manat Add Motor Acceleration, Deceleration
double spd_ref;  //Manat Add
double spd_ref_max = 481;  //60Hz Manat Add
double spd_ref_min = 20;  //2.5Hz Manat Add
double speed;   // Manat Add
unsigned char direction; // Manat Add rotation direction (0 forwared, 1 reverse)
unsigned char run;  //Manat Add
int num = 0; // Manat Add for Hz Show 7-Segments

const double refclk=31376.6;      // measured output frequency
//----------------------------------------------------------------------------
const int ledPin =  A5;         // the number of the LED pin
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
long interval = 50000;           // interval at which to blink (milliseconds)
//----------------------------------------------------------------------------

// variables used inside interrupt service declared as voilatile
volatile byte current_count;              // Keep track of where the current count is in sine 256 array
volatile byte ms4_delay;             //variable used to generate a 4ms delay
volatile byte c4ms;              // after every 4ms this variable is incremented, its used to create a delay of 1 second
volatile unsigned long phase_accumulator;   // pahse accumulator
volatile unsigned long tword_m;  // dds tuning word m, refer to DDS_calculator (from Martin Nawrath) for explination.

void setup()
{
  Serial.begin(9600);        // Manat Add
   
  pinMode(PWM1, OUTPUT);      //sets the digital pin as output
  pinMode(PWM2, OUTPUT);      //sets the digital pin as output
  pinMode(PWM3, OUTPUT);  //sets the digital pin as output
  
  pinMode(ledPin, OUTPUT);  //Manat Add
  pinMode(ISR_exec_time, OUTPUT);      //sets the digital pin as output
  pinMode(INVERTOR_ENABLE, OUTPUT);      //sets the digital pin as output
  digitalWrite(INVERTOR_ENABLE, LOW);  //Manat Add
 
  
  
  //sbi(PORTB,program_exec_time); //Sets the pin
  //digitalWrite(program_exec_time, HIGH);
  
  Setup_timer0();
  Setup_timer1();
  Setup_timer2();
  //Disable Timer 1 interrupt to avoid any timing delays
  //cbi (TIMSK0,TOIE0);              //disable Timer0 !!! delay() is now not available
  sbi (TIMSK2,TOIE2);              //enable Timer2 Interrupt
  
  
  tword_m=pow(2,32)*speed/refclk;  //calulate DDS new tuning word 
  
  //-----------SevenSegment-------------
  byte numDigits = 2;
  byte digitPins[] = {13, 12};
  byte segmentPins[] = {8, 7, 6, 5, 4, 3, 2};
  bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
  byte hardwareConfig = COMMON_CATHODE; // See README.md for options
  bool updateWithDelays = false; // Default. Recommended
  bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros
  
  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments, updateWithDelays, leadingZeros);
  sevseg.setBrightness(1);
  //------------------------------------
  digitalWrite(ledPin, HIGH);  
  WaitLoop(30000);
  digitalWrite(ledPin, LOW);  
}

void loop()
{
  while(1) 
  { 
     ReadAnalogs();
     unsigned long currentMillis = millis();    //  For ledState
     
   //---------Control Power IR2111----------------------------
     if (speed > spd_ref_min){
       offset_1 = 85;
       offset_2 = 170;
       run = 1;
       digitalWrite(INVERTOR_ENABLE, HIGH);
     }
     else {
       offset_1 = 0;
       offset_2 = 0;
       run = 0;
       digitalWrite(INVERTOR_ENABLE, LOW);
     }
   //---------7 Segments Show Hz------------------------------  
     num = (speed/8);
     if(speed == spd_ref_min) num = 0;
     sevseg.setNumber(num, 2);
     sevseg.refreshDisplay();    
   //sbi(PORTC,program_exec_time); //Sets the pin 
   //digitalWrite(program_exec_time, HIGH);
   
   //---------Monitor program---------------------------------
  if((currentMillis - previousMillis) > (interval/(num+1))) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
   
   //---------------------------------------------------------------
     
      if (c4ms > 0) // c4ms = 4ms, thus 4ms *250 = 1 second delay
       {                 
        c4ms=0;                          //Reset c4ms
        cbi (TIMSK2,TOIE2);              //Disable Timer2 Interrupt
        tword_m=pow(2,32)*speed/refclk;  //Calulate DDS new tuning word
        sbi (TIMSK2,TOIE2);              //Enable Timer2 Interrupt 

      }
      
  }
}

void WaitLoop(unsigned int time)
{
unsigned int i,j;
for (j=0;j<time;j++)
{
for (i=0;i<200;i++) //the ATmega is runs at 16MHz
if (PORTC==0xFF) DDRB|=0x02; //just a dummy instruction
}
}


void ReadAnalogs(void)
{
      spd_ref=map(analogRead(2),0,1023,0,spd_ref_max);             //Read voltage on analog 1 to see desired output frequency, 0V = 0Hz, 5V = 1.023kHz
      ad_cel=map(analogRead(3),0,1023,1,200);    // Manat Add
      
       if(spd_ref > spd_ref_max) spd_ref = spd_ref_max;  // Manat add maximum 60Hz  
       if(spd_ref < spd_ref_min) spd_ref = 0;  // Manat Add minimum 2.5Hz
  

       if (INPUT_DIR)
        {
          if (direction==0) spd_ref=spd_ref_min;
          if (speed==spd_ref_min) direction=1; //only allow direction change at minimum speed
          
        }
  
       else
       {
          if (direction==1) spd_ref=spd_ref_min;
          if (speed==spd_ref_min) direction=0; //only alow direction change at minimum speed
          
        }  

     //if (spd_ref>speed) speed=speed+0.02;   // Hz step
     //if (spd_ref<speed) speed=speed-0.02;
     if (spd_ref>speed) speed=speed+(1/ad_cel);   // Hz step
     if (spd_ref<speed) speed=speed-(1/ad_cel);
     if (speed<spd_ref_min) speed=spd_ref_min;
  
}


void Setup_timer0(void)
{
  
  
  TCCR0B = (TCCR0B & 0b11111000) | 0x02;
  // Timer1 PWM Mode set to Phase Correct PWM
  cbi (TCCR0A, COM0A0);
  sbi (TCCR0A, COM0A1);
  cbi (TCCR0A, COM0B0); 
  sbi (TCCR0A, COM0B1);

  // Mode 1 / Phase Correct PWM
  sbi (TCCR0A, WGM00); 
  cbi (TCCR0A, WGM01);
  
}

void Setup_timer1(void)
{
  
 TCCR1B = (TCCR1B & 0b11111000) |0x02;
  // Timer1 Clock Prescaler to : 1
  
  cbi (TCCR1A, COM1A0);
  sbi (TCCR1A, COM1A1);
  cbi (TCCR1A, COM1B0); 
  sbi (TCCR1A, COM1B1);

 
  sbi (TCCR1A, WGM10); 
  cbi (TCCR1A, WGM11);
  cbi (TCCR1B, WGM12);
  cbi (TCCR1B, WGM13);
}


void Setup_timer2() 
{
  
  TCCR2B = (TCCR2B & 0b11111000) | 0x02;// Timer2 Clock Prescaler to : 1
 
  cbi (TCCR2A, COM2A0);  // clear Compare Match
  sbi (TCCR2A, COM2A1);
  cbi (TCCR2A, COM2B0); 
  sbi (TCCR2A, COM2B1);
  
  // Mode 1  / Phase Correct PWM
  sbi (TCCR2A, WGM20);  
  cbi (TCCR2A, WGM21);
  cbi (TCCR2B, WGM22);
}



ISR(TIMER2_OVF_vect)
{
  //cbi(PORTC,program_exec_time); //Clear the pin
  //sbi(PORTC,ISR_exec_time);          // Sets the pin
  //digitalWrite(program_exec_time, LOW);
  digitalWrite(ISR_exec_time, HIGH);
  if (direction==0) 
     phase_accumulator=phase_accumulator+tword_m;
  else
     phase_accumulator=phase_accumulator-tword_m; 
     
   //phase_accumulator=phase_accumulator+tword_m; //Adds tuning M word to previoud phase accumulator. refer to DDS_calculator (from Martin Nawrath) for explination. 
   
  if (run==0)
    current_count=0; 
  else
    current_count=phase_accumulator >> 24;     // use upper 8 bits of phase_accumulator as frequency information 
  //-------------------------------

 
  //------------------------------
  
  OCR1A = pgm_read_byte_near(sine256 + current_count); // read value fron ROM sine table and send to PWM
  OCR1B = pgm_read_byte_near(sine256 + (uint8_t)(current_count + offset_1)); // read value fron ROM sine table and send to PWM, 120 Degree out of phase of PWM1
  OCR2A = pgm_read_byte_near(sine256 + (uint8_t)(current_count + offset_2));// read value fron ROM sine table and send to PWM, 120 Degree out of phase of PWM2
  
  //increment variable ms4_delay every 4mS/125 =  milliseconds 32uS
  if(ms4_delay++ == 125)
  
  {  
    c4ms++;
    ms4_delay=0; //reset count
   }   

//cbi(PORTC,ISR_exec_time);            //Clear the pin
digitalWrite(ISR_exec_time, LOW);
}

เศรษฐกิจดี MC3PHAC AND ATMEGA 328 3 PHASE INDUCTION MOTOR SPEED CONTRO...

// DDS Sine Generator 3 phase ATMEGA 168 328P PWM 4KHZ + Danijel Gorupec, 2015
// Support 7 Segments Show Hz  15/07/2017
// Import SevSeg Library : https://playground.arduino.cc/Main/SevenSegmentLibrary

#include "arduino.h" //Store data in flash (program) memory instead of SRAM
#include "avr/pgmspace.h"
#include "avr/io.h"
#include "SevSeg.h"
SevSeg sevseg; //Instantiate a seven segment controller object

   const byte sine256[] PROGMEM  = {
  127,130,133,136,139,143,146,149,152,155,158,161,164,167,170,173,176,178,181,184,187,190,192,195,198,200,203,205,208,210,212,215,217,219,221,223,225,227,229,231,233,234,236,238,239,240,
  242,243,244,245,247,248,249,249,250,251,252,252,253,253,253,254,254,254,254,254,254,254,253,253,253,252,252,251,250,249,249,248,247,245,244,243,242,240,239,238,236,234,233,231,229,227,225,223,
  221,219,217,215,212,210,208,205,203,200,198,195,192,190,187,184,181,178,176,173,170,167,164,161,158,155,152,149,146,143,139,136,133,130,127,124,121,118,115,111,108,105,102,99,96,93,90,87,84,81,78,
  76,73,70,67,64,62,59,56,54,51,49,46,44,42,39,37,35,33,31,29,27,25,23,21,20,18,16,15,14,12,11,10,9,7,6,5,5,4,3,2,2,1,1,1,0,0,0,0,0,0,0,1,1,1,2,2,3,4,5,5,6,7,9,10,11,12,14,15,16,18,20,21,23,25,27,29,31,
  33,35,37,39,42,44,46,49,51,54,56,59,62,64,67,70,73,76,78,81,84,87,90,93,96,99,102,105,108,111,115,118,121,124

};
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) //define a bit to have the properties of a clear bit operator
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))//define a bit to have the properties of a set bit operator
#define INPUT_DIR ((PINC&0x04)==0)  // 00000010 = A2 //Control Direction


int PWM1=  9;  //PWM1 output, phase 1
int PWM2 = 10; //PWM2 output, phase 2
int PWM3 = 11; //PWM3 output, phase 3


//int offset_1 = 85; //offset 1 is 120 degrees out of phase with previous phase, Refer to PWM to sine.xls
//int offset_2 = 170; //offset 2 is 120 degrees out of phase with offset 1. Refer to PWM to sine.xls
int offset_1; //offset 1 is 120 degrees out of phase with previous phase, Refer to PWM to sine.xls
int offset_2; //offset 2 is 120 degrees out of phase with offset 1. Refer to PWM to sine.xls
//int program_exec_time = A3; //monitor how quickly the interrupt trigger
int ISR_exec_time = A0; //monitor how long the interrupt takes
int INVERTOR_ENABLE = A1; //INVERTOR ENABLE

double ad_cel; //Manat Add Motor Acceleration, Deceleration
double spd_ref;  //Manat Add
double spd_ref_max = 481;  //60Hz Manat Add
double spd_ref_min = 20;  //2.5Hz Manat Add
double speed;   // Manat Add
unsigned char direction; // Manat Add rotation direction (0 forwared, 1 reverse)
unsigned char run;  //Manat Add
int num = 0; // Manat Add for Hz Show 7-Segments

const double refclk=31376.6;      // measured output frequency
//----------------------------------------------------------------------------
const int ledPin =  A5;         // the number of the LED pin
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
long interval = 50000;           // interval at which to blink (milliseconds)
//----------------------------------------------------------------------------

// variables used inside interrupt service declared as voilatile
volatile byte current_count;              // Keep track of where the current count is in sine 256 array
volatile byte ms4_delay;             //variable used to generate a 4ms delay
volatile byte c4ms;              // after every 4ms this variable is incremented, its used to create a delay of 1 second
volatile unsigned long phase_accumulator;   // pahse accumulator
volatile unsigned long tword_m;  // dds tuning word m, refer to DDS_calculator (from Martin Nawrath) for explination.

void setup()
{
  Serial.begin(9600);        // Manat Add
   
  pinMode(PWM1, OUTPUT);      //sets the digital pin as output
  pinMode(PWM2, OUTPUT);      //sets the digital pin as output
  pinMode(PWM3, OUTPUT);  //sets the digital pin as output
  
  pinMode(ledPin, OUTPUT);  //Manat Add
  pinMode(ISR_exec_time, OUTPUT);      //sets the digital pin as output
  pinMode(INVERTOR_ENABLE, OUTPUT);      //sets the digital pin as output
  digitalWrite(INVERTOR_ENABLE, LOW);  //Manat Add
 
  
  
  //sbi(PORTB,program_exec_time); //Sets the pin
  //digitalWrite(program_exec_time, HIGH);
  
  Setup_timer0();
  Setup_timer1();
  Setup_timer2();
  //Disable Timer 1 interrupt to avoid any timing delays
  //cbi (TIMSK0,TOIE0);              //disable Timer0 !!! delay() is now not available
  sbi (TIMSK2,TOIE2);              //enable Timer2 Interrupt
  
  
  tword_m=pow(2,32)*speed/refclk;  //calulate DDS new tuning word 
  
  //-----------SevenSegment-------------
  byte numDigits = 2;
  byte digitPins[] = {13, 12};
  byte segmentPins[] = {8, 7, 6, 5, 4, 3, 2};
  bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
  byte hardwareConfig = COMMON_CATHODE; // See README.md for options
  bool updateWithDelays = false; // Default. Recommended
  bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros
  
  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments, updateWithDelays, leadingZeros);
  sevseg.setBrightness(1);
  //------------------------------------
  digitalWrite(ledPin, HIGH);  
  WaitLoop(30000);
  digitalWrite(ledPin, LOW);  
}

void loop()
{
  while(1) 
  { 
     ReadAnalogs();
     unsigned long currentMillis = millis();    //  For ledState
     
   //---------Control Power IR2111----------------------------
     if (speed > spd_ref_min){
       offset_1 = 85;
       offset_2 = 170;
       run = 1;
       digitalWrite(INVERTOR_ENABLE, HIGH);
     }
     else {
       offset_1 = 0;
       offset_2 = 0;
       run = 0;
       digitalWrite(INVERTOR_ENABLE, LOW);
     }
   //---------7 Segments Show Hz------------------------------  
     num = (speed/8);
     if(speed == spd_ref_min) num = 0;
     sevseg.setNumber(num, 2);
     sevseg.refreshDisplay();    
   //sbi(PORTC,program_exec_time); //Sets the pin 
   //digitalWrite(program_exec_time, HIGH);
   
   //---------Monitor program---------------------------------
  if((currentMillis - previousMillis) > (interval/(num+1))) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
   
   //---------------------------------------------------------------
     
      if (c4ms > 0) // c4ms = 4ms, thus 4ms *250 = 1 second delay
       {                 
        c4ms=0;                          //Reset c4ms
        cbi (TIMSK2,TOIE2);              //Disable Timer2 Interrupt
        tword_m=pow(2,32)*speed/refclk;  //Calulate DDS new tuning word
        sbi (TIMSK2,TOIE2);              //Enable Timer2 Interrupt 

      }
      
  }
}

void WaitLoop(unsigned int time)
{
unsigned int i,j;
for (j=0;j<time;j++)
{
for (i=0;i<200;i++) //the ATmega is runs at 16MHz
if (PORTC==0xFF) DDRB|=0x02; //just a dummy instruction
}
}


void ReadAnalogs(void)
{
      spd_ref=map(analogRead(2),0,1023,0,spd_ref_max);             //Read voltage on analog 1 to see desired output frequency, 0V = 0Hz, 5V = 1.023kHz
      ad_cel=map(analogRead(3),0,1023,1,200);    // Manat Add
      
       if(spd_ref > spd_ref_max) spd_ref = spd_ref_max;  // Manat add maximum 60Hz  
       if(spd_ref < spd_ref_min) spd_ref = 0;  // Manat Add minimum 2.5Hz
  

       if (INPUT_DIR)
        {
          if (direction==0) spd_ref=spd_ref_min;
          if (speed==spd_ref_min) direction=1; //only allow direction change at minimum speed
          
        }
  
       else
       {
          if (direction==1) spd_ref=spd_ref_min;
          if (speed==spd_ref_min) direction=0; //only alow direction change at minimum speed
          
        }  

     //if (spd_ref>speed) speed=speed+0.02;   // Hz step
     //if (spd_ref<speed) speed=speed-0.02;
     if (spd_ref>speed) speed=speed+(1/ad_cel);   // Hz step
     if (spd_ref<speed) speed=speed-(1/ad_cel);
     if (speed<spd_ref_min) speed=spd_ref_min;
  
}


void Setup_timer0(void)
{
  
  
  TCCR0B = (TCCR0B & 0b11111000) | 0x02;
  // Timer1 PWM Mode set to Phase Correct PWM
  cbi (TCCR0A, COM0A0);
  sbi (TCCR0A, COM0A1);
  cbi (TCCR0A, COM0B0); 
  sbi (TCCR0A, COM0B1);

  // Mode 1 / Phase Correct PWM
  sbi (TCCR0A, WGM00); 
  cbi (TCCR0A, WGM01);
  
}

void Setup_timer1(void)
{
  
 TCCR1B = (TCCR1B & 0b11111000) |0x02;
  // Timer1 Clock Prescaler to : 1
  
  cbi (TCCR1A, COM1A0);
  sbi (TCCR1A, COM1A1);
  cbi (TCCR1A, COM1B0); 
  sbi (TCCR1A, COM1B1);

 
  sbi (TCCR1A, WGM10); 
  cbi (TCCR1A, WGM11);
  cbi (TCCR1B, WGM12);
  cbi (TCCR1B, WGM13);
}


void Setup_timer2() 
{
  
  TCCR2B = (TCCR2B & 0b11111000) | 0x02;// Timer2 Clock Prescaler to : 1
 
  cbi (TCCR2A, COM2A0);  // clear Compare Match
  sbi (TCCR2A, COM2A1);
  cbi (TCCR2A, COM2B0); 
  sbi (TCCR2A, COM2B1);
  
  // Mode 1  / Phase Correct PWM
  sbi (TCCR2A, WGM20);  
  cbi (TCCR2A, WGM21);
  cbi (TCCR2B, WGM22);
}



ISR(TIMER2_OVF_vect)
{
  //cbi(PORTC,program_exec_time); //Clear the pin
  //sbi(PORTC,ISR_exec_time);          // Sets the pin
  //digitalWrite(program_exec_time, LOW);
  digitalWrite(ISR_exec_time, HIGH);
  if (direction==0) 
     phase_accumulator=phase_accumulator+tword_m;
  else
     phase_accumulator=phase_accumulator-tword_m; 
     
   //phase_accumulator=phase_accumulator+tword_m; //Adds tuning M word to previoud phase accumulator. refer to DDS_calculator (from Martin Nawrath) for explination. 
   
  if (run==0)
    current_count=0; 
  else
    current_count=phase_accumulator >> 24;     // use upper 8 bits of phase_accumulator as frequency information 
  //-------------------------------

 
  //------------------------------
  
  OCR1A = pgm_read_byte_near(sine256 + current_count); // read value fron ROM sine table and send to PWM
  OCR1B = pgm_read_byte_near(sine256 + (uint8_t)(current_count + offset_1)); // read value fron ROM sine table and send to PWM, 120 Degree out of phase of PWM1
  OCR2A = pgm_read_byte_near(sine256 + (uint8_t)(current_count + offset_2));// read value fron ROM sine table and send to PWM, 120 Degree out of phase of PWM2
  
  //increment variable ms4_delay every 4mS/125 =  milliseconds 32uS
  if(ms4_delay++ == 125)
  
  {  
    c4ms++;
    ms4_delay=0; //reset count
   }   

//cbi(PORTC,ISR_exec_time);            //Clear the pin
digitalWrite(ISR_exec_time, LOW);
}

คนไทยรุ่นใหม่ เศรษฐกิจดี Three phase AC induction motor driver

// DDS Sine Generator 3 phase ATMEGA 168 328P PWM 4KHZ + Danijel Gorupec, 2015

// Support 7 Segments Show Hz  15/07/2017
// Import SevSeg Library : https://playground.arduino.cc/Main/SevenSegmentLibrary

#include "arduino.h" //Store data in flash (program) memory instead of SRAM
#include "avr/pgmspace.h"
#include "avr/io.h"
#include "SevSeg.h"
SevSeg sevseg; //Instantiate a seven segment controller object

   const byte sine256[] PROGMEM  = {
  127,130,133,136,139,143,146,149,152,155,158,161,164,167,170,173,176,178,181,184,187,190,192,195,198,200,203,205,208,210,212,215,217,219,221,223,225,227,229,231,233,234,236,238,239,240,
  242,243,244,245,247,248,249,249,250,251,252,252,253,253,253,254,254,254,254,254,254,254,253,253,253,252,252,251,250,249,249,248,247,245,244,243,242,240,239,238,236,234,233,231,229,227,225,223,
  221,219,217,215,212,210,208,205,203,200,198,195,192,190,187,184,181,178,176,173,170,167,164,161,158,155,152,149,146,143,139,136,133,130,127,124,121,118,115,111,108,105,102,99,96,93,90,87,84,81,78,
  76,73,70,67,64,62,59,56,54,51,49,46,44,42,39,37,35,33,31,29,27,25,23,21,20,18,16,15,14,12,11,10,9,7,6,5,5,4,3,2,2,1,1,1,0,0,0,0,0,0,0,1,1,1,2,2,3,4,5,5,6,7,9,10,11,12,14,15,16,18,20,21,23,25,27,29,31,
  33,35,37,39,42,44,46,49,51,54,56,59,62,64,67,70,73,76,78,81,84,87,90,93,96,99,102,105,108,111,115,118,121,124

};
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) //define a bit to have the properties of a clear bit operator
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))//define a bit to have the properties of a set bit operator
#define INPUT_DIR ((PINC&0x04)==0)  // 00000010 = A2 //Control Direction


int PWM1=  9;  //PWM1 output, phase 1
int PWM2 = 10; //PWM2 output, phase 2
int PWM3 = 11; //PWM3 output, phase 3


//int offset_1 = 85; //offset 1 is 120 degrees out of phase with previous phase, Refer to PWM to sine.xls
//int offset_2 = 170; //offset 2 is 120 degrees out of phase with offset 1. Refer to PWM to sine.xls
int offset_1; //offset 1 is 120 degrees out of phase with previous phase, Refer to PWM to sine.xls
int offset_2; //offset 2 is 120 degrees out of phase with offset 1. Refer to PWM to sine.xls
//int program_exec_time = A3; //monitor how quickly the interrupt trigger
int ISR_exec_time = A0; //monitor how long the interrupt takes
int INVERTOR_ENABLE = A1; //INVERTOR ENABLE

double ad_cel; //Manat Add Motor Acceleration, Deceleration
double spd_ref;  //Manat Add
double spd_ref_max = 481;  //60Hz Manat Add
double spd_ref_min = 20;  //2.5Hz Manat Add
double speed;   // Manat Add
unsigned char direction; // Manat Add rotation direction (0 forwared, 1 reverse)
unsigned char run;  //Manat Add
int num = 0; // Manat Add for Hz Show 7-Segments

const double refclk=31376.6;      // measured output frequency
//----------------------------------------------------------------------------
const int ledPin =  A5;         // the number of the LED pin
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
long interval = 50000;           // interval at which to blink (milliseconds)
//----------------------------------------------------------------------------

// variables used inside interrupt service declared as voilatile
volatile byte current_count;              // Keep track of where the current count is in sine 256 array
volatile byte ms4_delay;             //variable used to generate a 4ms delay
volatile byte c4ms;              // after every 4ms this variable is incremented, its used to create a delay of 1 second
volatile unsigned long phase_accumulator;   // pahse accumulator
volatile unsigned long tword_m;  // dds tuning word m, refer to DDS_calculator (from Martin Nawrath) for explination.

void setup()
{
  Serial.begin(9600);        // Manat Add
   
  pinMode(PWM1, OUTPUT);      //sets the digital pin as output
  pinMode(PWM2, OUTPUT);      //sets the digital pin as output
  pinMode(PWM3, OUTPUT);  //sets the digital pin as output
  
  pinMode(ledPin, OUTPUT);  //Manat Add
  pinMode(ISR_exec_time, OUTPUT);      //sets the digital pin as output
  pinMode(INVERTOR_ENABLE, OUTPUT);      //sets the digital pin as output
  digitalWrite(INVERTOR_ENABLE, LOW);  //Manat Add
 
  
  
  //sbi(PORTB,program_exec_time); //Sets the pin
  //digitalWrite(program_exec_time, HIGH);
  
  Setup_timer0();
  Setup_timer1();
  Setup_timer2();
  //Disable Timer 1 interrupt to avoid any timing delays
  //cbi (TIMSK0,TOIE0);              //disable Timer0 !!! delay() is now not available
  sbi (TIMSK2,TOIE2);              //enable Timer2 Interrupt
  
  
  tword_m=pow(2,32)*speed/refclk;  //calulate DDS new tuning word 
  
  //-----------SevenSegment-------------
  byte numDigits = 2;
  byte digitPins[] = {13, 12};
  byte segmentPins[] = {8, 7, 6, 5, 4, 3, 2};
  bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
  byte hardwareConfig = COMMON_CATHODE; // See README.md for options
  bool updateWithDelays = false; // Default. Recommended
  bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros
  
  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments, updateWithDelays, leadingZeros);
  sevseg.setBrightness(1);
  //------------------------------------
  digitalWrite(ledPin, HIGH);  
  WaitLoop(30000);
  digitalWrite(ledPin, LOW);  
}

void loop()
{
  while(1) 
  { 
     ReadAnalogs();
     unsigned long currentMillis = millis();    //  For ledState
     
   //---------Control Power IR2111----------------------------
     if (speed > spd_ref_min){
       offset_1 = 85;
       offset_2 = 170;
       run = 1;
       digitalWrite(INVERTOR_ENABLE, HIGH);
     }
     else {
       offset_1 = 0;
       offset_2 = 0;
       run = 0;
       digitalWrite(INVERTOR_ENABLE, LOW);
     }
   //---------7 Segments Show Hz------------------------------  
     num = (speed/8);
     if(speed == spd_ref_min) num = 0;
     sevseg.setNumber(num, 2);
     sevseg.refreshDisplay();    
   //sbi(PORTC,program_exec_time); //Sets the pin 
   //digitalWrite(program_exec_time, HIGH);
   
   //---------Monitor program---------------------------------
  if((currentMillis - previousMillis) > (interval/(num+1))) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
   
   //---------------------------------------------------------------
     
      if (c4ms > 0) // c4ms = 4ms, thus 4ms *250 = 1 second delay
       {                 
        c4ms=0;                          //Reset c4ms
        cbi (TIMSK2,TOIE2);              //Disable Timer2 Interrupt
        tword_m=pow(2,32)*speed/refclk;  //Calulate DDS new tuning word
        sbi (TIMSK2,TOIE2);              //Enable Timer2 Interrupt 

      }
      
  }
}

void WaitLoop(unsigned int time)
{
unsigned int i,j;
for (j=0;j<time;j++)
{
for (i=0;i<200;i++) //the ATmega is runs at 16MHz
if (PORTC==0xFF) DDRB|=0x02; //just a dummy instruction
}
}


void ReadAnalogs(void)
{
      spd_ref=map(analogRead(2),0,1023,0,spd_ref_max);             //Read voltage on analog 1 to see desired output frequency, 0V = 0Hz, 5V = 1.023kHz
      ad_cel=map(analogRead(3),0,1023,1,200);    // Manat Add
      
       if(spd_ref > spd_ref_max) spd_ref = spd_ref_max;  // Manat add maximum 60Hz  
       if(spd_ref < spd_ref_min) spd_ref = 0;  // Manat Add minimum 2.5Hz
  

       if (INPUT_DIR)
        {
          if (direction==0) spd_ref=spd_ref_min;
          if (speed==spd_ref_min) direction=1; //only allow direction change at minimum speed
          
        }
  
       else
       {
          if (direction==1) spd_ref=spd_ref_min;
          if (speed==spd_ref_min) direction=0; //only alow direction change at minimum speed
          
        }  

     //if (spd_ref>speed) speed=speed+0.02;   // Hz step
     //if (spd_ref<speed) speed=speed-0.02;
     if (spd_ref>speed) speed=speed+(1/ad_cel);   // Hz step
     if (spd_ref<speed) speed=speed-(1/ad_cel);
     if (speed<spd_ref_min) speed=spd_ref_min;
  
}


void Setup_timer0(void)
{
  
  
  TCCR0B = (TCCR0B & 0b11111000) | 0x02;
  // Timer1 PWM Mode set to Phase Correct PWM
  cbi (TCCR0A, COM0A0);
  sbi (TCCR0A, COM0A1);
  cbi (TCCR0A, COM0B0); 
  sbi (TCCR0A, COM0B1);

  // Mode 1 / Phase Correct PWM
  sbi (TCCR0A, WGM00); 
  cbi (TCCR0A, WGM01);
  
}

void Setup_timer1(void)
{
  
 TCCR1B = (TCCR1B & 0b11111000) |0x02;
  // Timer1 Clock Prescaler to : 1
  
  cbi (TCCR1A, COM1A0);
  sbi (TCCR1A, COM1A1);
  cbi (TCCR1A, COM1B0); 
  sbi (TCCR1A, COM1B1);

 
  sbi (TCCR1A, WGM10); 
  cbi (TCCR1A, WGM11);
  cbi (TCCR1B, WGM12);
  cbi (TCCR1B, WGM13);
}


void Setup_timer2() 
{
  
  TCCR2B = (TCCR2B & 0b11111000) | 0x02;// Timer2 Clock Prescaler to : 1
 
  cbi (TCCR2A, COM2A0);  // clear Compare Match
  sbi (TCCR2A, COM2A1);
  cbi (TCCR2A, COM2B0); 
  sbi (TCCR2A, COM2B1);
  
  // Mode 1  / Phase Correct PWM
  sbi (TCCR2A, WGM20);  
  cbi (TCCR2A, WGM21);
  cbi (TCCR2B, WGM22);
}



ISR(TIMER2_OVF_vect)
{
  //cbi(PORTC,program_exec_time); //Clear the pin
  //sbi(PORTC,ISR_exec_time);          // Sets the pin
  //digitalWrite(program_exec_time, LOW);
  digitalWrite(ISR_exec_time, HIGH);
  if (direction==0) 
     phase_accumulator=phase_accumulator+tword_m;
  else
     phase_accumulator=phase_accumulator-tword_m; 
     
   //phase_accumulator=phase_accumulator+tword_m; //Adds tuning M word to previoud phase accumulator. refer to DDS_calculator (from Martin Nawrath) for explination. 
   
  if (run==0)
    current_count=0; 
  else
    current_count=phase_accumulator >> 24;     // use upper 8 bits of phase_accumulator as frequency information 
  //-------------------------------

 
  //------------------------------
  
  OCR1A = pgm_read_byte_near(sine256 + current_count); // read value fron ROM sine table and send to PWM
  OCR1B = pgm_read_byte_near(sine256 + (uint8_t)(current_count + offset_1)); // read value fron ROM sine table and send to PWM, 120 Degree out of phase of PWM1
  OCR2A = pgm_read_byte_near(sine256 + (uint8_t)(current_count + offset_2));// read value fron ROM sine table and send to PWM, 120 Degree out of phase of PWM2
  
  //increment variable ms4_delay every 4mS/125 =  milliseconds 32uS
  if(ms4_delay++ == 125)
  
  {  
    c4ms++;
    ms4_delay=0; //reset count
   }   

//cbi(PORTC,ISR_exec_time);            //Clear the pin
digitalWrite(ISR_exec_time, LOW);
}

thailand 4.0 ผู้วิจัยพัฒนาออกแบบ Three phase AC induction motor driver

// DDS Sine Generator 3 phase ATMEGA 168 328P PWM 4KHZ + Danijel Gorupec, 2015

// Support 7 Segments Show Hz  15/07/2017
// Import SevSeg Library : https://playground.arduino.cc/Main/SevenSegmentLibrary

#include "arduino.h" //Store data in flash (program) memory instead of SRAM
#include "avr/pgmspace.h"
#include "avr/io.h"
#include "SevSeg.h"
SevSeg sevseg; //Instantiate a seven segment controller object

   const byte sine256[] PROGMEM  = {
  127,130,133,136,139,143,146,149,152,155,158,161,164,167,170,173,176,178,181,184,187,190,192,195,198,200,203,205,208,210,212,215,217,219,221,223,225,227,229,231,233,234,236,238,239,240,
  242,243,244,245,247,248,249,249,250,251,252,252,253,253,253,254,254,254,254,254,254,254,253,253,253,252,252,251,250,249,249,248,247,245,244,243,242,240,239,238,236,234,233,231,229,227,225,223,
  221,219,217,215,212,210,208,205,203,200,198,195,192,190,187,184,181,178,176,173,170,167,164,161,158,155,152,149,146,143,139,136,133,130,127,124,121,118,115,111,108,105,102,99,96,93,90,87,84,81,78,
  76,73,70,67,64,62,59,56,54,51,49,46,44,42,39,37,35,33,31,29,27,25,23,21,20,18,16,15,14,12,11,10,9,7,6,5,5,4,3,2,2,1,1,1,0,0,0,0,0,0,0,1,1,1,2,2,3,4,5,5,6,7,9,10,11,12,14,15,16,18,20,21,23,25,27,29,31,
  33,35,37,39,42,44,46,49,51,54,56,59,62,64,67,70,73,76,78,81,84,87,90,93,96,99,102,105,108,111,115,118,121,124

};
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) //define a bit to have the properties of a clear bit operator
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))//define a bit to have the properties of a set bit operator
#define INPUT_DIR ((PINC&0x04)==0)  // 00000010 = A2 //Control Direction


int PWM1=  9;  //PWM1 output, phase 1
int PWM2 = 10; //PWM2 output, phase 2
int PWM3 = 11; //PWM3 output, phase 3


//int offset_1 = 85; //offset 1 is 120 degrees out of phase with previous phase, Refer to PWM to sine.xls
//int offset_2 = 170; //offset 2 is 120 degrees out of phase with offset 1. Refer to PWM to sine.xls
int offset_1; //offset 1 is 120 degrees out of phase with previous phase, Refer to PWM to sine.xls
int offset_2; //offset 2 is 120 degrees out of phase with offset 1. Refer to PWM to sine.xls
//int program_exec_time = A3; //monitor how quickly the interrupt trigger
int ISR_exec_time = A0; //monitor how long the interrupt takes
int INVERTOR_ENABLE = A1; //INVERTOR ENABLE

double ad_cel; //Manat Add Motor Acceleration, Deceleration
double spd_ref;  //Manat Add
double spd_ref_max = 481;  //60Hz Manat Add
double spd_ref_min = 20;  //2.5Hz Manat Add
double speed;   // Manat Add
unsigned char direction; // Manat Add rotation direction (0 forwared, 1 reverse)
unsigned char run;  //Manat Add
int num = 0; // Manat Add for Hz Show 7-Segments

const double refclk=31376.6;      // measured output frequency
//----------------------------------------------------------------------------
const int ledPin =  A5;         // the number of the LED pin
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
long interval = 50000;           // interval at which to blink (milliseconds)
//----------------------------------------------------------------------------

// variables used inside interrupt service declared as voilatile
volatile byte current_count;              // Keep track of where the current count is in sine 256 array
volatile byte ms4_delay;             //variable used to generate a 4ms delay
volatile byte c4ms;              // after every 4ms this variable is incremented, its used to create a delay of 1 second
volatile unsigned long phase_accumulator;   // pahse accumulator
volatile unsigned long tword_m;  // dds tuning word m, refer to DDS_calculator (from Martin Nawrath) for explination.

void setup()
{
  Serial.begin(9600);        // Manat Add
   
  pinMode(PWM1, OUTPUT);      //sets the digital pin as output
  pinMode(PWM2, OUTPUT);      //sets the digital pin as output
  pinMode(PWM3, OUTPUT);  //sets the digital pin as output
  
  pinMode(ledPin, OUTPUT);  //Manat Add
  pinMode(ISR_exec_time, OUTPUT);      //sets the digital pin as output
  pinMode(INVERTOR_ENABLE, OUTPUT);      //sets the digital pin as output
  digitalWrite(INVERTOR_ENABLE, LOW);  //Manat Add
 
  
  
  //sbi(PORTB,program_exec_time); //Sets the pin
  //digitalWrite(program_exec_time, HIGH);
  
  Setup_timer0();
  Setup_timer1();
  Setup_timer2();
  //Disable Timer 1 interrupt to avoid any timing delays
  //cbi (TIMSK0,TOIE0);              //disable Timer0 !!! delay() is now not available
  sbi (TIMSK2,TOIE2);              //enable Timer2 Interrupt
  
  
  tword_m=pow(2,32)*speed/refclk;  //calulate DDS new tuning word 
  
  //-----------SevenSegment-------------
  byte numDigits = 2;
  byte digitPins[] = {13, 12};
  byte segmentPins[] = {8, 7, 6, 5, 4, 3, 2};
  bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
  byte hardwareConfig = COMMON_CATHODE; // See README.md for options
  bool updateWithDelays = false; // Default. Recommended
  bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros
  
  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments, updateWithDelays, leadingZeros);
  sevseg.setBrightness(1);
  //------------------------------------
  digitalWrite(ledPin, HIGH);  
  WaitLoop(30000);
  digitalWrite(ledPin, LOW);  
}

void loop()
{
  while(1) 
  { 
     ReadAnalogs();
     unsigned long currentMillis = millis();    //  For ledState
     
   //---------Control Power IR2111----------------------------
     if (speed > spd_ref_min){
       offset_1 = 85;
       offset_2 = 170;
       run = 1;
       digitalWrite(INVERTOR_ENABLE, HIGH);
     }
     else {
       offset_1 = 0;
       offset_2 = 0;
       run = 0;
       digitalWrite(INVERTOR_ENABLE, LOW);
     }
   //---------7 Segments Show Hz------------------------------  
     num = (speed/8);
     if(speed == spd_ref_min) num = 0;
     sevseg.setNumber(num, 2);
     sevseg.refreshDisplay();    
   //sbi(PORTC,program_exec_time); //Sets the pin 
   //digitalWrite(program_exec_time, HIGH);
   
   //---------Monitor program---------------------------------
  if((currentMillis - previousMillis) > (interval/(num+1))) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
   
   //---------------------------------------------------------------
     
      if (c4ms > 0) // c4ms = 4ms, thus 4ms *250 = 1 second delay
       {                 
        c4ms=0;                          //Reset c4ms
        cbi (TIMSK2,TOIE2);              //Disable Timer2 Interrupt
        tword_m=pow(2,32)*speed/refclk;  //Calulate DDS new tuning word
        sbi (TIMSK2,TOIE2);              //Enable Timer2 Interrupt 

      }
      
  }
}

void WaitLoop(unsigned int time)
{
unsigned int i,j;
for (j=0;j<time;j++)
{
for (i=0;i<200;i++) //the ATmega is runs at 16MHz
if (PORTC==0xFF) DDRB|=0x02; //just a dummy instruction
}
}


void ReadAnalogs(void)
{
      spd_ref=map(analogRead(2),0,1023,0,spd_ref_max);             //Read voltage on analog 1 to see desired output frequency, 0V = 0Hz, 5V = 1.023kHz
      ad_cel=map(analogRead(3),0,1023,1,200);    // Manat Add
      
       if(spd_ref > spd_ref_max) spd_ref = spd_ref_max;  // Manat add maximum 60Hz  
       if(spd_ref < spd_ref_min) spd_ref = 0;  // Manat Add minimum 2.5Hz
  

       if (INPUT_DIR)
        {
          if (direction==0) spd_ref=spd_ref_min;
          if (speed==spd_ref_min) direction=1; //only allow direction change at minimum speed
          
        }
  
       else
       {
          if (direction==1) spd_ref=spd_ref_min;
          if (speed==spd_ref_min) direction=0; //only alow direction change at minimum speed
          
        }  

     //if (spd_ref>speed) speed=speed+0.02;   // Hz step
     //if (spd_ref<speed) speed=speed-0.02;
     if (spd_ref>speed) speed=speed+(1/ad_cel);   // Hz step
     if (spd_ref<speed) speed=speed-(1/ad_cel);
     if (speed<spd_ref_min) speed=spd_ref_min;
  
}


void Setup_timer0(void)
{
  
  
  TCCR0B = (TCCR0B & 0b11111000) | 0x02;
  // Timer1 PWM Mode set to Phase Correct PWM
  cbi (TCCR0A, COM0A0);
  sbi (TCCR0A, COM0A1);
  cbi (TCCR0A, COM0B0); 
  sbi (TCCR0A, COM0B1);

  // Mode 1 / Phase Correct PWM
  sbi (TCCR0A, WGM00); 
  cbi (TCCR0A, WGM01);
  
}

void Setup_timer1(void)
{
  
 TCCR1B = (TCCR1B & 0b11111000) |0x02;
  // Timer1 Clock Prescaler to : 1
  
  cbi (TCCR1A, COM1A0);
  sbi (TCCR1A, COM1A1);
  cbi (TCCR1A, COM1B0); 
  sbi (TCCR1A, COM1B1);

 
  sbi (TCCR1A, WGM10); 
  cbi (TCCR1A, WGM11);
  cbi (TCCR1B, WGM12);
  cbi (TCCR1B, WGM13);
}


void Setup_timer2() 
{
  
  TCCR2B = (TCCR2B & 0b11111000) | 0x02;// Timer2 Clock Prescaler to : 1
 
  cbi (TCCR2A, COM2A0);  // clear Compare Match
  sbi (TCCR2A, COM2A1);
  cbi (TCCR2A, COM2B0); 
  sbi (TCCR2A, COM2B1);
  
  // Mode 1  / Phase Correct PWM
  sbi (TCCR2A, WGM20);  
  cbi (TCCR2A, WGM21);
  cbi (TCCR2B, WGM22);
}



ISR(TIMER2_OVF_vect)
{
  //cbi(PORTC,program_exec_time); //Clear the pin
  //sbi(PORTC,ISR_exec_time);          // Sets the pin
  //digitalWrite(program_exec_time, LOW);
  digitalWrite(ISR_exec_time, HIGH);
  if (direction==0) 
     phase_accumulator=phase_accumulator+tword_m;
  else
     phase_accumulator=phase_accumulator-tword_m; 
     
   //phase_accumulator=phase_accumulator+tword_m; //Adds tuning M word to previoud phase accumulator. refer to DDS_calculator (from Martin Nawrath) for explination. 
   
  if (run==0)
    current_count=0; 
  else
    current_count=phase_accumulator >> 24;     // use upper 8 bits of phase_accumulator as frequency information 
  //-------------------------------

 
  //------------------------------
  
  OCR1A = pgm_read_byte_near(sine256 + current_count); // read value fron ROM sine table and send to PWM
  OCR1B = pgm_read_byte_near(sine256 + (uint8_t)(current_count + offset_1)); // read value fron ROM sine table and send to PWM, 120 Degree out of phase of PWM1
  OCR2A = pgm_read_byte_near(sine256 + (uint8_t)(current_count + offset_2));// read value fron ROM sine table and send to PWM, 120 Degree out of phase of PWM2
  
  //increment variable ms4_delay every 4mS/125 =  milliseconds 32uS
  if(ms4_delay++ == 125)
  
  {  
    c4ms++;
    ms4_delay=0; //reset count
   }   

//cbi(PORTC,ISR_exec_time);            //Clear the pin
digitalWrite(ISR_exec_time, LOW);
}

เศรษฐกิจปี 2560 ฟื้นฟูเศรษฐกิจ Arduino uno 3phase

// DDS Sine Generator 3 phase ATMEGA 168 328P PWM 4KHZ + Danijel Gorupec, 2015

// Support 7 Segments Show Hz  15/07/2017
// Import SevSeg Library : https://playground.arduino.cc/Main/SevenSegmentLibrary

#include "arduino.h" //Store data in flash (program) memory instead of SRAM
#include "avr/pgmspace.h"
#include "avr/io.h"
#include "SevSeg.h"
SevSeg sevseg; //Instantiate a seven segment controller object

   const byte sine256[] PROGMEM  = {
  127,130,133,136,139,143,146,149,152,155,158,161,164,167,170,173,176,178,181,184,187,190,192,195,198,200,203,205,208,210,212,215,217,219,221,223,225,227,229,231,233,234,236,238,239,240,
  242,243,244,245,247,248,249,249,250,251,252,252,253,253,253,254,254,254,254,254,254,254,253,253,253,252,252,251,250,249,249,248,247,245,244,243,242,240,239,238,236,234,233,231,229,227,225,223,
  221,219,217,215,212,210,208,205,203,200,198,195,192,190,187,184,181,178,176,173,170,167,164,161,158,155,152,149,146,143,139,136,133,130,127,124,121,118,115,111,108,105,102,99,96,93,90,87,84,81,78,
  76,73,70,67,64,62,59,56,54,51,49,46,44,42,39,37,35,33,31,29,27,25,23,21,20,18,16,15,14,12,11,10,9,7,6,5,5,4,3,2,2,1,1,1,0,0,0,0,0,0,0,1,1,1,2,2,3,4,5,5,6,7,9,10,11,12,14,15,16,18,20,21,23,25,27,29,31,
  33,35,37,39,42,44,46,49,51,54,56,59,62,64,67,70,73,76,78,81,84,87,90,93,96,99,102,105,108,111,115,118,121,124

};
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) //define a bit to have the properties of a clear bit operator
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))//define a bit to have the properties of a set bit operator
#define INPUT_DIR ((PINC&0x04)==0)  // 00000010 = A2 //Control Direction


int PWM1=  9;  //PWM1 output, phase 1
int PWM2 = 10; //PWM2 output, phase 2
int PWM3 = 11; //PWM3 output, phase 3


//int offset_1 = 85; //offset 1 is 120 degrees out of phase with previous phase, Refer to PWM to sine.xls
//int offset_2 = 170; //offset 2 is 120 degrees out of phase with offset 1. Refer to PWM to sine.xls
int offset_1; //offset 1 is 120 degrees out of phase with previous phase, Refer to PWM to sine.xls
int offset_2; //offset 2 is 120 degrees out of phase with offset 1. Refer to PWM to sine.xls
//int program_exec_time = A3; //monitor how quickly the interrupt trigger
int ISR_exec_time = A0; //monitor how long the interrupt takes
int INVERTOR_ENABLE = A1; //INVERTOR ENABLE

double ad_cel; //Manat Add Motor Acceleration, Deceleration
double spd_ref;  //Manat Add
double spd_ref_max = 481;  //60Hz Manat Add
double spd_ref_min = 20;  //2.5Hz Manat Add
double speed;   // Manat Add
unsigned char direction; // Manat Add rotation direction (0 forwared, 1 reverse)
unsigned char run;  //Manat Add
int num = 0; // Manat Add for Hz Show 7-Segments

const double refclk=31376.6;      // measured output frequency
//----------------------------------------------------------------------------
const int ledPin =  A5;         // the number of the LED pin
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
long interval = 50000;           // interval at which to blink (milliseconds)
//----------------------------------------------------------------------------

// variables used inside interrupt service declared as voilatile
volatile byte current_count;              // Keep track of where the current count is in sine 256 array
volatile byte ms4_delay;             //variable used to generate a 4ms delay
volatile byte c4ms;              // after every 4ms this variable is incremented, its used to create a delay of 1 second
volatile unsigned long phase_accumulator;   // pahse accumulator
volatile unsigned long tword_m;  // dds tuning word m, refer to DDS_calculator (from Martin Nawrath) for explination.

void setup()
{
  Serial.begin(9600);        // Manat Add
   
  pinMode(PWM1, OUTPUT);      //sets the digital pin as output
  pinMode(PWM2, OUTPUT);      //sets the digital pin as output
  pinMode(PWM3, OUTPUT);  //sets the digital pin as output
  
  pinMode(ledPin, OUTPUT);  //Manat Add
  pinMode(ISR_exec_time, OUTPUT);      //sets the digital pin as output
  pinMode(INVERTOR_ENABLE, OUTPUT);      //sets the digital pin as output
  digitalWrite(INVERTOR_ENABLE, LOW);  //Manat Add
 
  
  
  //sbi(PORTB,program_exec_time); //Sets the pin
  //digitalWrite(program_exec_time, HIGH);
  
  Setup_timer0();
  Setup_timer1();
  Setup_timer2();
  //Disable Timer 1 interrupt to avoid any timing delays
  //cbi (TIMSK0,TOIE0);              //disable Timer0 !!! delay() is now not available
  sbi (TIMSK2,TOIE2);              //enable Timer2 Interrupt
  
  
  tword_m=pow(2,32)*speed/refclk;  //calulate DDS new tuning word 
  
  //-----------SevenSegment-------------
  byte numDigits = 2;
  byte digitPins[] = {13, 12};
  byte segmentPins[] = {8, 7, 6, 5, 4, 3, 2};
  bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
  byte hardwareConfig = COMMON_CATHODE; // See README.md for options
  bool updateWithDelays = false; // Default. Recommended
  bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros
  
  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments, updateWithDelays, leadingZeros);
  sevseg.setBrightness(1);
  //------------------------------------
  digitalWrite(ledPin, HIGH);  
  WaitLoop(30000);
  digitalWrite(ledPin, LOW);  
}

void loop()
{
  while(1) 
  { 
     ReadAnalogs();
     unsigned long currentMillis = millis();    //  For ledState
     
   //---------Control Power IR2111----------------------------
     if (speed > spd_ref_min){
       offset_1 = 85;
       offset_2 = 170;
       run = 1;
       digitalWrite(INVERTOR_ENABLE, HIGH);
     }
     else {
       offset_1 = 0;
       offset_2 = 0;
       run = 0;
       digitalWrite(INVERTOR_ENABLE, LOW);
     }
   //---------7 Segments Show Hz------------------------------  
     num = (speed/8);
     if(speed == spd_ref_min) num = 0;
     sevseg.setNumber(num, 2);
     sevseg.refreshDisplay();    
   //sbi(PORTC,program_exec_time); //Sets the pin 
   //digitalWrite(program_exec_time, HIGH);
   
   //---------Monitor program---------------------------------
  if((currentMillis - previousMillis) > (interval/(num+1))) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
   
   //---------------------------------------------------------------
     
      if (c4ms > 0) // c4ms = 4ms, thus 4ms *250 = 1 second delay
       {                 
        c4ms=0;                          //Reset c4ms
        cbi (TIMSK2,TOIE2);              //Disable Timer2 Interrupt
        tword_m=pow(2,32)*speed/refclk;  //Calulate DDS new tuning word
        sbi (TIMSK2,TOIE2);              //Enable Timer2 Interrupt 

      }
      
  }
}

void WaitLoop(unsigned int time)
{
unsigned int i,j;
for (j=0;j<time;j++)
{
for (i=0;i<200;i++) //the ATmega is runs at 16MHz
if (PORTC==0xFF) DDRB|=0x02; //just a dummy instruction
}
}


void ReadAnalogs(void)
{
      spd_ref=map(analogRead(2),0,1023,0,spd_ref_max);             //Read voltage on analog 1 to see desired output frequency, 0V = 0Hz, 5V = 1.023kHz
      ad_cel=map(analogRead(3),0,1023,1,200);    // Manat Add
      
       if(spd_ref > spd_ref_max) spd_ref = spd_ref_max;  // Manat add maximum 60Hz  
       if(spd_ref < spd_ref_min) spd_ref = 0;  // Manat Add minimum 2.5Hz
  

       if (INPUT_DIR)
        {
          if (direction==0) spd_ref=spd_ref_min;
          if (speed==spd_ref_min) direction=1; //only allow direction change at minimum speed
          
        }
  
       else
       {
          if (direction==1) spd_ref=spd_ref_min;
          if (speed==spd_ref_min) direction=0; //only alow direction change at minimum speed
          
        }  

     //if (spd_ref>speed) speed=speed+0.02;   // Hz step
     //if (spd_ref<speed) speed=speed-0.02;
     if (spd_ref>speed) speed=speed+(1/ad_cel);   // Hz step
     if (spd_ref<speed) speed=speed-(1/ad_cel);
     if (speed<spd_ref_min) speed=spd_ref_min;
  
}


void Setup_timer0(void)
{
  
  
  TCCR0B = (TCCR0B & 0b11111000) | 0x02;
  // Timer1 PWM Mode set to Phase Correct PWM
  cbi (TCCR0A, COM0A0);
  sbi (TCCR0A, COM0A1);
  cbi (TCCR0A, COM0B0); 
  sbi (TCCR0A, COM0B1);

  // Mode 1 / Phase Correct PWM
  sbi (TCCR0A, WGM00); 
  cbi (TCCR0A, WGM01);
  
}

void Setup_timer1(void)
{
  
 TCCR1B = (TCCR1B & 0b11111000) |0x02;
  // Timer1 Clock Prescaler to : 1
  
  cbi (TCCR1A, COM1A0);
  sbi (TCCR1A, COM1A1);
  cbi (TCCR1A, COM1B0); 
  sbi (TCCR1A, COM1B1);

 
  sbi (TCCR1A, WGM10); 
  cbi (TCCR1A, WGM11);
  cbi (TCCR1B, WGM12);
  cbi (TCCR1B, WGM13);
}


void Setup_timer2() 
{
  
  TCCR2B = (TCCR2B & 0b11111000) | 0x02;// Timer2 Clock Prescaler to : 1
 
  cbi (TCCR2A, COM2A0);  // clear Compare Match
  sbi (TCCR2A, COM2A1);
  cbi (TCCR2A, COM2B0); 
  sbi (TCCR2A, COM2B1);
  
  // Mode 1  / Phase Correct PWM
  sbi (TCCR2A, WGM20);  
  cbi (TCCR2A, WGM21);
  cbi (TCCR2B, WGM22);
}



ISR(TIMER2_OVF_vect)
{
  //cbi(PORTC,program_exec_time); //Clear the pin
  //sbi(PORTC,ISR_exec_time);          // Sets the pin
  //digitalWrite(program_exec_time, LOW);
  digitalWrite(ISR_exec_time, HIGH);
  if (direction==0) 
     phase_accumulator=phase_accumulator+tword_m;
  else
     phase_accumulator=phase_accumulator-tword_m; 
     
   //phase_accumulator=phase_accumulator+tword_m; //Adds tuning M word to previoud phase accumulator. refer to DDS_calculator (from Martin Nawrath) for explination. 
   
  if (run==0)
    current_count=0; 
  else
    current_count=phase_accumulator >> 24;     // use upper 8 bits of phase_accumulator as frequency information 
  //-------------------------------

 
  //------------------------------
  
  OCR1A = pgm_read_byte_near(sine256 + current_count); // read value fron ROM sine table and send to PWM
  OCR1B = pgm_read_byte_near(sine256 + (uint8_t)(current_count + offset_1)); // read value fron ROM sine table and send to PWM, 120 Degree out of phase of PWM1
  OCR2A = pgm_read_byte_near(sine256 + (uint8_t)(current_count + offset_2));// read value fron ROM sine table and send to PWM, 120 Degree out of phase of PWM2
  
  //increment variable ms4_delay every 4mS/125 =  milliseconds 32uS
  if(ms4_delay++ == 125)
  
  {  
    c4ms++;
    ms4_delay=0; //reset count
   }   

//cbi(PORTC,ISR_exec_time);            //Clear the pin
digitalWrite(ISR_exec_time, LOW);
}

เศรษฐกิจปี 2560 ฟื้นฟูเศรษฐกิจ Arduino uno 3phase

// DDS Sine Generator 3 phase ATMEGA 168 328P PWM 4KHZ + Danijel Gorupec, 2015

// Support 7 Segments Show Hz  15/07/2017
// Import SevSeg Library : https://playground.arduino.cc/Main/SevenSegmentLibrary

#include "arduino.h" //Store data in flash (program) memory instead of SRAM
#include "avr/pgmspace.h"
#include "avr/io.h"
#include "SevSeg.h"
SevSeg sevseg; //Instantiate a seven segment controller object

   const byte sine256[] PROGMEM  = {
  127,130,133,136,139,143,146,149,152,155,158,161,164,167,170,173,176,178,181,184,187,190,192,195,198,200,203,205,208,210,212,215,217,219,221,223,225,227,229,231,233,234,236,238,239,240,
  242,243,244,245,247,248,249,249,250,251,252,252,253,253,253,254,254,254,254,254,254,254,253,253,253,252,252,251,250,249,249,248,247,245,244,243,242,240,239,238,236,234,233,231,229,227,225,223,
  221,219,217,215,212,210,208,205,203,200,198,195,192,190,187,184,181,178,176,173,170,167,164,161,158,155,152,149,146,143,139,136,133,130,127,124,121,118,115,111,108,105,102,99,96,93,90,87,84,81,78,
  76,73,70,67,64,62,59,56,54,51,49,46,44,42,39,37,35,33,31,29,27,25,23,21,20,18,16,15,14,12,11,10,9,7,6,5,5,4,3,2,2,1,1,1,0,0,0,0,0,0,0,1,1,1,2,2,3,4,5,5,6,7,9,10,11,12,14,15,16,18,20,21,23,25,27,29,31,
  33,35,37,39,42,44,46,49,51,54,56,59,62,64,67,70,73,76,78,81,84,87,90,93,96,99,102,105,108,111,115,118,121,124

};
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) //define a bit to have the properties of a clear bit operator
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))//define a bit to have the properties of a set bit operator
#define INPUT_DIR ((PINC&0x04)==0)  // 00000010 = A2 //Control Direction


int PWM1=  9;  //PWM1 output, phase 1
int PWM2 = 10; //PWM2 output, phase 2
int PWM3 = 11; //PWM3 output, phase 3


//int offset_1 = 85; //offset 1 is 120 degrees out of phase with previous phase, Refer to PWM to sine.xls
//int offset_2 = 170; //offset 2 is 120 degrees out of phase with offset 1. Refer to PWM to sine.xls
int offset_1; //offset 1 is 120 degrees out of phase with previous phase, Refer to PWM to sine.xls
int offset_2; //offset 2 is 120 degrees out of phase with offset 1. Refer to PWM to sine.xls
//int program_exec_time = A3; //monitor how quickly the interrupt trigger
int ISR_exec_time = A0; //monitor how long the interrupt takes
int INVERTOR_ENABLE = A1; //INVERTOR ENABLE

double ad_cel; //Manat Add Motor Acceleration, Deceleration
double spd_ref;  //Manat Add
double spd_ref_max = 481;  //60Hz Manat Add
double spd_ref_min = 20;  //2.5Hz Manat Add
double speed;   // Manat Add
unsigned char direction; // Manat Add rotation direction (0 forwared, 1 reverse)
unsigned char run;  //Manat Add
int num = 0; // Manat Add for Hz Show 7-Segments

const double refclk=31376.6;      // measured output frequency
//----------------------------------------------------------------------------
const int ledPin =  A5;         // the number of the LED pin
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
long interval = 50000;           // interval at which to blink (milliseconds)
//----------------------------------------------------------------------------

// variables used inside interrupt service declared as voilatile
volatile byte current_count;              // Keep track of where the current count is in sine 256 array
volatile byte ms4_delay;             //variable used to generate a 4ms delay
volatile byte c4ms;              // after every 4ms this variable is incremented, its used to create a delay of 1 second
volatile unsigned long phase_accumulator;   // pahse accumulator
volatile unsigned long tword_m;  // dds tuning word m, refer to DDS_calculator (from Martin Nawrath) for explination.

void setup()
{
  Serial.begin(9600);        // Manat Add
   
  pinMode(PWM1, OUTPUT);      //sets the digital pin as output
  pinMode(PWM2, OUTPUT);      //sets the digital pin as output
  pinMode(PWM3, OUTPUT);  //sets the digital pin as output
  
  pinMode(ledPin, OUTPUT);  //Manat Add
  pinMode(ISR_exec_time, OUTPUT);      //sets the digital pin as output
  pinMode(INVERTOR_ENABLE, OUTPUT);      //sets the digital pin as output
  digitalWrite(INVERTOR_ENABLE, LOW);  //Manat Add
 
  
  
  //sbi(PORTB,program_exec_time); //Sets the pin
  //digitalWrite(program_exec_time, HIGH);
  
  Setup_timer0();
  Setup_timer1();
  Setup_timer2();
  //Disable Timer 1 interrupt to avoid any timing delays
  //cbi (TIMSK0,TOIE0);              //disable Timer0 !!! delay() is now not available
  sbi (TIMSK2,TOIE2);              //enable Timer2 Interrupt
  
  
  tword_m=pow(2,32)*speed/refclk;  //calulate DDS new tuning word 
  
  //-----------SevenSegment-------------
  byte numDigits = 2;
  byte digitPins[] = {13, 12};
  byte segmentPins[] = {8, 7, 6, 5, 4, 3, 2};
  bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
  byte hardwareConfig = COMMON_CATHODE; // See README.md for options
  bool updateWithDelays = false; // Default. Recommended
  bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros
  
  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments, updateWithDelays, leadingZeros);
  sevseg.setBrightness(1);
  //------------------------------------
  digitalWrite(ledPin, HIGH);  
  WaitLoop(30000);
  digitalWrite(ledPin, LOW);  
}

void loop()
{
  while(1) 
  { 
     ReadAnalogs();
     unsigned long currentMillis = millis();    //  For ledState
     
   //---------Control Power IR2111----------------------------
     if (speed > spd_ref_min){
       offset_1 = 85;
       offset_2 = 170;
       run = 1;
       digitalWrite(INVERTOR_ENABLE, HIGH);
     }
     else {
       offset_1 = 0;
       offset_2 = 0;
       run = 0;
       digitalWrite(INVERTOR_ENABLE, LOW);
     }
   //---------7 Segments Show Hz------------------------------  
     num = (speed/8);
     if(speed == spd_ref_min) num = 0;
     sevseg.setNumber(num, 2);
     sevseg.refreshDisplay();    
   //sbi(PORTC,program_exec_time); //Sets the pin 
   //digitalWrite(program_exec_time, HIGH);
   
   //---------Monitor program---------------------------------
  if((currentMillis - previousMillis) > (interval/(num+1))) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
   
   //---------------------------------------------------------------
     
      if (c4ms > 0) // c4ms = 4ms, thus 4ms *250 = 1 second delay
       {                 
        c4ms=0;                          //Reset c4ms
        cbi (TIMSK2,TOIE2);              //Disable Timer2 Interrupt
        tword_m=pow(2,32)*speed/refclk;  //Calulate DDS new tuning word
        sbi (TIMSK2,TOIE2);              //Enable Timer2 Interrupt 

      }
      
  }
}

void WaitLoop(unsigned int time)
{
unsigned int i,j;
for (j=0;j<time;j++)
{
for (i=0;i<200;i++) //the ATmega is runs at 16MHz
if (PORTC==0xFF) DDRB|=0x02; //just a dummy instruction
}
}


void ReadAnalogs(void)
{
      spd_ref=map(analogRead(2),0,1023,0,spd_ref_max);             //Read voltage on analog 1 to see desired output frequency, 0V = 0Hz, 5V = 1.023kHz
      ad_cel=map(analogRead(3),0,1023,1,200);    // Manat Add
      
       if(spd_ref > spd_ref_max) spd_ref = spd_ref_max;  // Manat add maximum 60Hz  
       if(spd_ref < spd_ref_min) spd_ref = 0;  // Manat Add minimum 2.5Hz
  

       if (INPUT_DIR)
        {
          if (direction==0) spd_ref=spd_ref_min;
          if (speed==spd_ref_min) direction=1; //only allow direction change at minimum speed
          
        }
  
       else
       {
          if (direction==1) spd_ref=spd_ref_min;
          if (speed==spd_ref_min) direction=0; //only alow direction change at minimum speed
          
        }  

     //if (spd_ref>speed) speed=speed+0.02;   // Hz step
     //if (spd_ref<speed) speed=speed-0.02;
     if (spd_ref>speed) speed=speed+(1/ad_cel);   // Hz step
     if (spd_ref<speed) speed=speed-(1/ad_cel);
     if (speed<spd_ref_min) speed=spd_ref_min;
  
}


void Setup_timer0(void)
{
  
  
  TCCR0B = (TCCR0B & 0b11111000) | 0x02;
  // Timer1 PWM Mode set to Phase Correct PWM
  cbi (TCCR0A, COM0A0);
  sbi (TCCR0A, COM0A1);
  cbi (TCCR0A, COM0B0); 
  sbi (TCCR0A, COM0B1);

  // Mode 1 / Phase Correct PWM
  sbi (TCCR0A, WGM00); 
  cbi (TCCR0A, WGM01);
  
}

void Setup_timer1(void)
{
  
 TCCR1B = (TCCR1B & 0b11111000) |0x02;
  // Timer1 Clock Prescaler to : 1
  
  cbi (TCCR1A, COM1A0);
  sbi (TCCR1A, COM1A1);
  cbi (TCCR1A, COM1B0); 
  sbi (TCCR1A, COM1B1);

 
  sbi (TCCR1A, WGM10); 
  cbi (TCCR1A, WGM11);
  cbi (TCCR1B, WGM12);
  cbi (TCCR1B, WGM13);
}


void Setup_timer2() 
{
  
  TCCR2B = (TCCR2B & 0b11111000) | 0x02;// Timer2 Clock Prescaler to : 1
 
  cbi (TCCR2A, COM2A0);  // clear Compare Match
  sbi (TCCR2A, COM2A1);
  cbi (TCCR2A, COM2B0); 
  sbi (TCCR2A, COM2B1);
  
  // Mode 1  / Phase Correct PWM
  sbi (TCCR2A, WGM20);  
  cbi (TCCR2A, WGM21);
  cbi (TCCR2B, WGM22);
}



ISR(TIMER2_OVF_vect)
{
  //cbi(PORTC,program_exec_time); //Clear the pin
  //sbi(PORTC,ISR_exec_time);          // Sets the pin
  //digitalWrite(program_exec_time, LOW);
  digitalWrite(ISR_exec_time, HIGH);
  if (direction==0) 
     phase_accumulator=phase_accumulator+tword_m;
  else
     phase_accumulator=phase_accumulator-tword_m; 
     
   //phase_accumulator=phase_accumulator+tword_m; //Adds tuning M word to previoud phase accumulator. refer to DDS_calculator (from Martin Nawrath) for explination. 
   
  if (run==0)
    current_count=0; 
  else
    current_count=phase_accumulator >> 24;     // use upper 8 bits of phase_accumulator as frequency information 
  //-------------------------------

 
  //------------------------------
  
  OCR1A = pgm_read_byte_near(sine256 + current_count); // read value fron ROM sine table and send to PWM
  OCR1B = pgm_read_byte_near(sine256 + (uint8_t)(current_count + offset_1)); // read value fron ROM sine table and send to PWM, 120 Degree out of phase of PWM1
  OCR2A = pgm_read_byte_near(sine256 + (uint8_t)(current_count + offset_2));// read value fron ROM sine table and send to PWM, 120 Degree out of phase of PWM2
  
  //increment variable ms4_delay every 4mS/125 =  milliseconds 32uS
  if(ms4_delay++ == 125)
  
  {  
    c4ms++;
    ms4_delay=0; //reset count
   }   

//cbi(PORTC,ISR_exec_time);            //Clear the pin
digitalWrite(ISR_exec_time, LOW);
}